2013-05-06 57 views
2

我是IOS新手,我正在做的是我只是簡單地在我的主視圖中放置了一個UIButton,我希望當我點擊該按鈕時,我應該帶我到下一個堆棧。因爲我正在做的是PushViewController導航到下一個堆棧

-(IBAction)stack:(id)sender{ 
Page1 *button =[[Page1 alloc]init]; 
[self.navigationController pushViewController:button animated:YES]; 
} 

我只是在我的代碼中添加...但它不工作。

我需要把UINavigationController主視圖或AppDelegate按下按鈕,導航創建新的堆棧

中的AppDelegate

回答

2

self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; 

,改變你按下按鈕的方法,

-(IBAction)stack:(id)sender{ 
    Page1 *button =[[Page1 alloc] initWithNibName:@"Page1" bundle:nil]; 
    [self.navigationController pushViewController:button animated:YES]; 
} 
+0

雅感謝您的幫助和..can你告訴我,爲什麼我們的appDelegate添加UINavigationController的工作原理? – user2353519 2013-05-06 05:54:45

+0

如果我們需要按下按鈕並創建一個沒有UINavigationController的新堆棧,那我們能做什麼? – user2353519 2013-05-06 05:55:45

+0

Page1 * button = [[Page1 alloc] initWithNibName:@「Page1」bundle:nil]; [self presentViewController:button animated:YES completion:nil]; – Ushan87 2013-05-06 06:01:53

0

試試這個

-(void)aboutButtonPressed { 
    AboutViewController *aboutView =[[AboutViewController alloc] initWithNibName:@"About" bundle:nil]; 
    [self.navigationController pushViewController:aboutView animated:YES]; 
    [aboutView release]; 
} 
+0

只需添加nibname ...如何可以我導航..如果我沒有UINavigationController在appdelegate? – user2353519 2013-05-06 06:07:15

+0

如果您正在使用storyboard,只需嵌入導航控制器或在appdelegate中創建其實例 – 2013-05-06 06:25:54

0

是的,你需要編寫以下行appdel

self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease]; 
navigationController=[[UINavigationController alloc] initWithRootViewController:self.viewController]; 
self.window.rootViewController = navigationController; 
0

請做以下的事情: 在AppDelegate.h

@property (nonatomic, retain) UINavigationController *navigationController; 

在AppDelegate.m

@implementation AppDelegate 

@synthesize navigationController; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
     self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil]; 
    } else { 
     self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil]; 
    } 
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; 

    self.navigationController.navigationBar.barStyle = UIBarStyleBlack; 
    self.window.rootViewController = self.navigationController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

當你想要推下一個viewcontroller,你可以寫上面的代碼:

-(void)fontButtonPress{ 
     FontViewController *fontViewController = [[FontViewController alloc] initWithNibName:@"FontViewController_iPhone" bundle:nil]; 
    [self.navigationController pushViewController:fontViewController animated:YES]; 
} 

讓我知道你是否有任何問題。

感謝