xcode IBOutlet UITableView 在 View 加载后为 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6724461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:30:10  来源:igfitidea点击:

IBOutlet UITableView is null after View did load

iphoneobjective-cxcodecocoa-touch

提问by Rene Koller

Here's the basic code (based on Xcode's Tabbed Applicaion Template)

这是基本代码(基于 Xcode 的选项卡式应用程序模板)

ViewController.h

视图控制器.h

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic,retain) NSMutableArray *movies;
@property (nonatomic,retain) IBOutlet UITableView *tableView;

ViewController.m

视图控制器.m

@implementation ViewController
@synthesize movies,tableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Watchlist", @"Watchlist");
        self.tabBarItem.image = [UIImage imageNamed:@"watchlist"];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
     NSLog(@"tableView = %@", tableView);
}

Output

输出

tableView = (null)

The TableView is connected to File's owner in IB with class is set to ViewController

TableView 连接到 IB 中的文件所有者,类设置为 ViewController

I really don't get why the tableView is null. I'm not a complete newbie to Cocoa (but to the iPhone SDK), I created a Single View based Application with a TableView dataSource to see if I was missing something. I got it working in under a minute.

我真的不明白为什么 tableView 为空。我不是 Cocoa 的完全新手(而是 iPhone SDK),我使用 TableView 数据源创建了一个基于单视图的应用程序,看看我是否遗漏了什么。不到一分钟我就搞定了。

Anybody can help out?

有人可以帮忙吗?

回答by Paul.s

In interface builder right click File's Ownerand ensure that the following connections are made:

在界面生成器中右键单击File's Owner并确保进行以下连接:

Outlets
tableView - Table View

Referencing Outlets
dateSource - Table View
delegate   - Table View

I suspect you may have not made the first connection?

我怀疑你可能没有建立第一次连接?

回答by auco

Make sure the nib/xib is included in your current target.

确保 nib/xib 包含在您当前的目标中。

I just experienced this issue on Xcode5 with iOS7 SDK. Strangely enough, I discovered that the nib wasn't included in my target anymore. I don't know when and why that happened, but it had this strange side effect that most IBOutlets weren't set up properly, even if all connections from code to nib/xib were fine.

我刚刚在带有 iOS7 SDK 的 Xcode5 上遇到了这个问题。奇怪的是,我发现笔尖不再包含在我的目标中。我不知道发生这种情况的时间和原因,但它有一个奇怪的副作用,即大多数 IBOutlets 没有正确设置,即使从代码到 nib/xib 的所有连接都很好。

For example: my MKMapView *mapwas in my list of subviews on viewDidLoad:, but the IBOutlet MKMapView *mapproperty in my view controller was still nil. After I ticked the "include in target" checkbox, everything worked as expected.

例如: myMKMapView *map在我的子视图列表中viewDidLoad:,但IBOutlet MKMapView *map我的视图控制器中的属性仍然是nil。在我勾选“包含在目标中”复选框后,一切都按预期工作。