[英]load .xib file and display it
我試圖在按下按鈕時在我的應用程序中顯示“關於頁面”,但是當我按下UIButton時,將調用NSLog(@“ Button按鈕按下”),但視圖不會加載。 這是我的代碼:
- (IBAction)pushAbout {
NSLog(@"Button pressed");
AboutView * aboutView = [[AboutView alloc] initWithNibName:@"AboutView" bundle:nil];
[self.navigationController pushViewController:aboutView animated:YES];
[aboutView release];
}
我已經在示例中看到了此代碼,但是它是在UITableViewController中而不是在Ordianry UIView控制器中調用的。
將類文件“ AboutView”創建為UIViewController子類。 他們被創造了並且保持不變
一個猜測:您的視圖不在UINavigationController中,因此
self.navigationController
實際上是nil
,什么也沒有發生。
解決方案是將主視圖放置在導航控制器中,並將導航控制器的視圖添加到應用程序的主窗口中。
一種簡單的方法:
在您的應用程序委托中,更改
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
至
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions
(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UINavigationController* navController = [[UINavigationController alloc]
initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
或類似。 請注意,這僅是入門,可能不是最好的方法。 (我假設您剛剛在xcode中創建了基於視圖的應用程序。)
是的,您沒有設置UINavigationController。
加
NSLog(@"nav? %@", self.navigationController);
進行操作,您會看到它轉儲了(空)。
但是,如果輸入以下代碼,AboutView.xib可以正常工作:
[self presentModalViewController:aboutView animated:YES];
代替
[self.navigationController pushViewController:aboutView animated:YES];
該視圖將顯示。 在您的壓縮示例中,AboutView.xib不包含標簽,因此不要懷疑它是否是白頁。
您可以使用以下方法關閉顯示的模態視圖
[self dismissModalViewControllerAnimated:YES];
在AboutViewController中。
為了獲得UINavigationController的幫助,建議您使用基於導航的應用程序模板創建一個應用程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.