【问题标题】:Can't set any IBOutlet无法设置任何 IBOutlet
【发布时间】:2014-07-31 17:16:40
【问题描述】:

我有一个UIViewController 实例化另一个UIViewController(带有NIB)并将其推送到屏幕上。然后我尝试设置UILabel 并让UIWebView 加载URL。然而它不起作用!没有错误!界面看起来就像在 NIB 中一样,没有编程措施将 UILabel 设置为不同的 @"TestString" 工作。

有人可以指出我的问题吗?我似乎无法解决它。

谢谢。

实例化 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

实例化 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

实例化部分:

- (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];
}

更新:已确认连接

【问题讨论】:

  • _testLabel = [[UILabel alloc] init]; 你重新初始化了一个由笔尖设置的标签。 [webView setTestLabel:label]; 你初始化了一个新标签,并将 web 视图的标签设置为此。这是您尝试删除默认标签的两倍。

标签: ios xcode uiviewcontroller uikit iboutlet


【解决方案1】:

奇怪的是更多的人没有这个问题。

你不能在这些 IBOutlets 上设置属性的原因是这些 UI 对象还没有被初始化。

在这个阶段你只是初始化了 ViewController。

所以你需要做的是创建一个将在你的标签中使用的字符串。

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

在这里设置:

- (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];

}

然后:

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

如果您使用带有转场的情节提要,同样适用:

- (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";
}

【讨论】:

  • 你是对的。我正在等待有人找到答案:D
【解决方案2】:

我尝试了所有现有的答案,但没有找到任何有用的答案。 现在我发现以下链接:New instance of a UIViewController within another UIViewController: Why can't I set an instance variable?

据我了解(如果我错了,请纠正我 - 这对我很重要)在视图实际加载之前我无法设置任何 outlets 值!!!

这意味着如果您尝试在

之前将值设置为 Outlets

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

已执行,则上述所有值将被[super viewDidLoad]; 覆盖,因此没有值会反映任何更改。

据我所知,需要做的是 - 定义不是 Outlets 的变量并为其分配所需的值。在- (void) viewDidLoad { [super viewDidLoad]; } 方法中,在 super 之后将上述定义的值分配给受尊重的 Outlets。

请确认或更正我的回答!非常感谢。

【讨论】:

    【解决方案3】:
    1. 可能您没有连接插座。尝试将 CTRL 从 Interface Builder 中的 UI 控件拖动到 View Controller。

    2. 另一种可能的解决方案是选择控件,打开右侧面板的最后一个选项卡并在那里查找连接。

    【讨论】:

      【解决方案4】:

      您可能需要连接您的 IBOutlets。正确连接后,它们在源代码中应如下所示:左侧的小灰点应为每个连接的 IBOutlet 填充。

      【讨论】:

        【解决方案5】:

        检查这个答案 这将帮助您您需要在文件中设置 FileOwner..

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

        【讨论】:

          猜你喜欢
          • 2014-11-29
          • 1970-01-01
          • 2021-12-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多