简体   繁体   中英

Can't set any IBOutlet

I have a UIViewController that instantiates another UIViewController (with NIB) and pushes it to the screen. I then try to set UILabel and get UIWebView to load URL. However it doesn't work! No errors! Interface looks just like in NIB and no programmatic measures to set UILabel to a different @"TestString" work.

Can someone point me to the issue? I just can't seem to resolve it.

Thank you.

Instantiated UIViewController - WebView.h:

#import <UIKit/UIKit.h>

@interface WebView : UIViewController {

}

@property (strong, nonatomic) IBOutlet UILabel *testLabel;
@property (strong, nonatomic) IBOutlet UIWebView *webView;

- (void) openWeb: (NSString *) url;

@end

Instantiated UIViewController - WebView.m:

#import "WebView.h"

@interface WebView ()

@end

@implementation WebView

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _testLabel = [[UILabel alloc] init];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) setTestLabel:(UILabel *)theTestLabel {
    NSLog(@"Called set Label: %@", [theTestLabel text]);
    _testLabel = theTestLabel;
}

- (void) openWeb: (NSString *) url {
    NSLog(@"openWeb with url %@", url);
    [_testLabel setText:url];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[[NSURL alloc] initWithString:url]];
    [_webView loadRequest:urlRequest];
}

@end

Instantiating Part:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    ScheduledClass *c = [classes objectAtIndex:[indexPath row]];
    WebView *webView;
    webView = [[WebView alloc] initWithNibName:@"WebView" bundle:nil];
    UILabel *label = [[UILabel alloc] init];
    label.text = @"Testing";
    [webView setTestLabel:label];
    [webView openWeb:@"http://meirz.net"];

    [self.navigationController pushViewController:webView animated:YES];
}

Update: Confirmed connections

在此处输入图片说明在此处输入图片说明在此处输入图片说明

It's odd that more people don't have this issue.

The reason why you can't set the properties on these IBOutlets is that these UI objects haven't been initialised yet.

You've only initialised the ViewController by this stage.

So what you need to do is create a string that will be used in your label.

@property (strong, nonatomic) IBOutlet UILabel *label;
@property (strong, nonatomic) NSString *stringForLabel;

Set it here:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    WebView *webView = [[WebView alloc] initWithNibName:@"WebView" bundle:nil];
    webView.stringForLabel = @"My Label String";    
    [self.navigationController pushViewController:webView animated:YES];

}

Then:

- (void)viewDidLoad {
   [super viewDidLoad];
   self.label.text = self.stringForLabel;
}

Same applies if you're using storyboards with segues:

- (void)buttonPress:(id)sender {
   [self performSegueWithIdentifier:@"mySegue" sender:self];
}

- (void)prepareForSegue(UIStoryboardSegue *)segue sender:(id)sender {
   AController *myController = [segue destinationViewController];
   myController.stringForLabel = @"My Label String";
}

I tried all of the existing answers and didn't find anything that would help. Now what I discovered is following link: New instance of a UIViewController within another UIViewController: Why can't I set an instance variable?

As far as I understood (please correct me if I am wrong - it is important to me) I can't set any outlets value before the view is actually loaded!!!

What it means is that if you are trying to set values to Outlets before

- (void)viewDidLoad { [super viewDidLoad]; }

executed, then all the above values will be overwritten by [super viewDidLoad]; and therefore no values will reflect any changes.

As far as I can figure, what needs to be done is - define variables that are not Outlets and assign desired values to them. In - (void) viewDidLoad { [super viewDidLoad]; } - (void) viewDidLoad { [super viewDidLoad]; } method, after super you assign above defined values to respected Outlets.

Please confirm or correct my answer! Thank you very much.

  1. Possibly you have not connected the outlets. Try CTRL dragging from the UI controls in the Interface Builder to the View Controller.

  2. Another possible solution would be to select the control, open the last tab of the right pannel and look for the connection there.

You might need to hook up your IBOutlets. When they're correctly connected, they should look like this in your source code: The little gray dots on the left side should be filled in for each connected IBOutlet.

在此处输入图片说明

Check this ans This will help you You need to set FileOwner in your file..

https://stackoverflow.com/a/12568803/563848

在此处输入图片说明在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM