简体   繁体   English

无法设置任何IBOutlet

[英]Can't set any IBOutlet

I have a UIViewController that instantiates another UIViewController (with NIB) and pushes it to the screen. 我有一个UIViewController ,它实例化另一个UIViewController (带有NIB)并将其推送到屏幕。 I then try to set UILabel and get UIWebView to load URL. 然后,我尝试设置UILabel并获取UIWebView来加载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. 接口看起来就像在NIB中一样,没有任何编程措施可以将UILabel设置为其他@"TestString"工作。

Can someone point me to the issue? 有人可以指出我的问题吗? I just can't seem to resolve it. 我似乎无法解决它。

Thank you. 谢谢。

Instantiated UIViewController - WebView.h: 实例化的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: 实例化的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. 您无法在这些IBOutlets上设置属性的原因是,这些UI对象尚未初始化。

You've only initialised the ViewController by this stage. 您仅在此阶段初始化了ViewController。

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? 现在,我发现的是以下链接: 另一个UIViewController中的UIViewController的新实例:为什么不能设置实例变量?

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 这意味着如果您尝试在之前将值设置为Outlets

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

executed, then all the above values will be overwritten by [super viewDidLoad]; 执行后,以上所有值将被[super viewDidLoad];覆盖[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. 据我所知,需要做的是-定义不是Outlet的变量,并为其分配所需的值。 In - (void) viewDidLoad { [super viewDidLoad]; } - (void) viewDidLoad { [super viewDidLoad]; } - (void) viewDidLoad { [super viewDidLoad]; } method, after super you assign above defined values to respected Outlets. - (void) viewDidLoad { [super viewDidLoad]; }方法,在超级之后,您将上述定义的值分配给受尊重的插座。

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. 尝试将CTRL从Interface Builder中的UI控件拖动到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. 您可能需要连接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. 正确连接后,它们在源代码中应如下所示:每个连接的IBOutlet的左侧小灰点都应填充。

在此处输入图片说明

Check this ans This will help you You need to set FileOwner in your file.. 选中此选项,这将对您有所帮助。您需要在文件中设置FileOwner

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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