![](/img/trans.png)
[英]Why can't I set UIViewController's imageView in other UIViewController?
[英]Why can't I edit the properties of UIViewController in Interface Builder when it's the File's Owner?
我創建了一個新的“ Tab Bar應用程序”項目,該項目為我提供了FirstViewController.xib和SecondViewController.xib。
在AppDelegate
,選項卡欄控制器的配置如下:
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
這顯然是從nib文件加載UIViewController
的實例,不是嗎? 但是,當我打開SecondViewController.xib時,我看到的唯一根級對象是視圖控制器的視圖。 這對我來說沒有任何意義,因為我相信整個UIViewController
實例實際上都來自該筆尖。
其余在哪里? 為什么看不到標題的UIViewController
屬性? 當我單擊“文件所有者”時,“身份”選項卡顯示“ SecondViewController”,因此“文件所有者”絕對是UIViewController
但似乎沒有保存在筆尖中。 實際上,實例似乎是通過編程方式創建的,而筆尖似乎僅存檔視圖。
這很混亂,因為nib文件的名稱是SecondViewController.xib。 有人可以澄清嗎?
當對象是“筆尖文件所有者”時,意味着該對象在裝載筆尖之前已經實例化(不一定“初始化”)。 當您裝入筆尖時,它並不是在創建所有者,而只是將插座連接到所有者。
我同意,您無法為使用IB中的“屬性”面板通過initWithNibName初始化的視圖控制器設置屬性的初始值,這令人困惑。
但是,我相信您可以通過文件所有者的IB中的“用戶定義的運行時屬性”面板在文件所有者上設置屬性值(僅XCode 4.2+,僅iOS5 +)。
您只能查看/更改“ Object
部分中的“ Object
某些屬性。
通常,在對象部分,其是對象被設定為IBoutlet
的文件所有者( UIViewController
你的情況),如果你拖動UIViewController
到對象部分,您可以編輯為好,但在談到文件的所有者,您可以設置其變量/屬性/方法,以及僅是IBOutlets
或IBAction
那些變量/屬性/方法。
如果要設置其標題等,則必須通過代碼手動進行。
實際上,SecondViewController的實例來自SecondViewController.h。 當您使用initWithNibName:@“ SecondViewController”時,它將加載筆尖及其屬性,但是,如果要更改這些對象中的任何一個,則需要使用接口構建器將它們連接到SecondViewController。
重現您的問題,我可以使用以下視圖獲取SecondViewController.xib:
視圖標簽-第二視圖文本視圖
如果您查看SecondViewController.m,則可以在方法中看到其更改標題
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"Second", @"Second");
self.tabBarItem.image = [UIImage imageNamed:@"second"];
}
return self;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.