簡體   English   中英

從另一個UIViewController推送UIViewController-如何?

[英]Pushing a UIViewController from another UIViewController - How to?

我有一個UIViewController子類(說BBB),該子類繼承自已編寫的自定義UIViewController類(說AAA)。 BBB類中具有UITableView,並且當用戶點擊UITableView的任何單元格時,我想推送另一個UIViewController類(說CCC)。

因此,我嘗試將CCC控制器推入BBB的tableView: didSelectRowAtIndexPath:方法中。

我的代碼是

CCC *ccc = [[CCC alloc] init];
[self.navigationController pushViewController:ccc animated:YES];
[ccc release];

點擊單元格時什么也沒有發生,所以我通過以下代碼檢查了BBB類的naviagtion控制器。

if (self.navigationController == nil)
{
    NSLog(@"Navigation controller is nil");
}

消息已成功打印:)因此,我嘗試將一些內容分配給導航控制器,但很不幸,這是一個只讀屬性。

然后,我嘗試通過將ccc分配為rootviewcontroller來制作本地UINavigationController,然后嘗試推送該本地導航控制器。 它引發異常“不支持多次按下同一視圖控制器實例”。

我的問題是

  • 是否可以找到導航控制器在AAA級中獲得零值的地方? 我沒有在BBB類中將導航控制器設置為nil,並且在AAA類中發現是否有任何類似“ self.navigationController = nil”的語句。 但是,沒有什么是那樣的。

  • 我該如何參加CCC課程?

謝謝

將其放入您的應用程序委托中,您的代碼的第一個版本應該可以工作。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window makeKeyAndVisible];
    AAA *aaa = [[AAA alloc] init];
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController: aaa];
    [self.window makeKeyAndVisible];

    return YES;
}

為了正確設置self.navigationController屬性,您必須使用[[UINavigationController alloc] initWithRootViewController: aaa]創建一個UINavigationController 然后,無論您當前在何處添加aaa(splitViewController或UIWindow等),都可以使用您創建的導航控制器。

例如,如果要使用代碼設置視圖,請在應用程序委托中:

AAA *aaa = [[AAA alloc] init];
UINavigationController *root = [[UINavigationController alloc] initWithRootViewController: aaa];
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[window makeKeyAndVisible];
window.rootViewController = root;

現在, aaa.navigationController將指向root ,因此您可以執行原始帖子中的操作: [self.navigationController pushViewController: bbb animated: YES];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM