2011-11-15 29 views
7

Tôi có một dự án iPhone hiện không có UINavigationController trong đó. Thay vào đó, tôi trao đổi chế độ xem bằng cách tự xử lý cấu trúc phân cấp chế độ xem. Tuy nhiên - như bạn có thể nhận thức - đây là thực hành không tốt, vì vậy tôi đang cố gắng tìm ra cách triển khai một UINavigationController vào ứng dụng hiện tại của mình. Tôi rõ ràng có thể bắt đầu dự án của tôi hơn và sử dụng một mẫu Xcode, nhưng tôi thực sự không muốn làm điều này.iPhone SDK - Thêm UINavigationController Programmatically

Phải có cách để thực hiện việc này theo lập trình. Nhưng tôi không thể tìm ra mã nào cần đi trong AppDelegate. Có ai có bất cứ kinh nghiệm với điều này? Tôi đang thua lỗ vào lúc này.

Chúc mừng, Brett

Trả lời

18

Yeap.

- (void)applicationDidFinishLaunching:(UIApplication *)application 
{ 
    navController=[[UINavigationController alloc] init]; 
    MyViewController *firstController=[[MyViewController alloc] init]; 
    [navController pushViewController:firstController animated:NO]; 
    [window addSubview: navController.view]; 
} 
- (void) dealloc 
{ 
    ... 
    [navController release]; 
    ... 
} 

Đó là một câu hỏi khá phổ biến, có một cái nhìn lúc này quá .... Programmatically add UINavigationController in UIViewController

+0

Tôi nghĩ bạn nên đặt hoạt ảnh: có. bởi vì hầu hết thời gian. chúng tôi muốn hoạt ảnh của bộ điều khiển trước khi chuyển sang chế độ xem mới. cảm ơn :) – hqt

1

trong Xcode 4 trong trình soạn thảo giao diện, mở bộ điều khiển xem ban đầu, và sau đó chọn 'biên tập> nhúng trong> navigation controller'

1

ví dụ về việc tạo ra và phát hành một bộ điều khiển chuyển hướng:

UINavigationController *navCon = [[UINavigationController alloc] init]; 
[navCon pushViewController:yourViewController animated:NO]; 
[navCon release]; 
0

tôi giải quyết cùng một vấn đề Sử dụng cái này. !

- (void)applicationDidFinishLaunching:(UIApplication *)application 
{ 
     navController=[[UINavigationController alloc] init]; 
     MyViewController *firstController=[[MyViewController alloc] init]; 
     self.window.rootViewController = firstController; 
     [self.window addSubview navController.view]; 

} 
7
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease]; 

// application.applicationIconBadgeNumber = 0; 
navController=[[UINavigationController alloc]initWithRootViewController:self.viewController]; 
self.window.rootViewController = navController; 
[self.window makeKeyAndVisible]; 
1

[tôi đã thêm điều khiển chuyển hướng vào dự án thanh tab của tôi và nó đang làm việc một cách hoàn hảo

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 


UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease]; 

UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease]; 

UIViewController *viewController3 = [[[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil] autorelease]; 

self.tabBarController = [[[UITabBarController alloc] init] autorelease]; 


self.tabBarController.viewControllers = @[viewController1, viewController2,viewController3]; 


self.navigationController= [[UINavigationController alloc]initWithRootViewController:self.tabBarController]; 

// self.window.rootViewController = self.tabBarController;

self.window.rootViewController = self.navigationController; 


[self.window makeKeyAndVisible]; 


return YES; 
Các vấn đề liên quan