2012-06-29 74 views
0

在Xcode 4.2中,我知道可以使用storyboard中的一個按鈕進入另一個視圖。XCode 4.2點擊按鈕切換到另一個視圖

但是說我想故事板,但不是使用segue push,而是編碼我自己的按鈕來顯示另一個視圖,怎麼做? 我必須包含導航控制器嗎?

編輯:添加的源代碼

AppDelegate.h

// 
// AppDelegate.h 
// Sandbox1 
// 
// Created on 6/29/12. 
// Copyright (c) 2012 __MyCompanyName__. All rights reserved. 
// 

#import <UIKit/UIKit.h> 
#import "ViewController.h" 

@interface AppDelegate : UIResponder <UIApplicationDelegate> 

@property (strong, nonatomic) UIWindow *window; 
@property (strong, nonatomic) ViewController *viewController; 
@property (nonatomic, retain) UINavigationController *navController; 

@end 

AppDelegate.m

// 
// AppDelegate.m 
// Sandbox1 
// 
// Created on 6/29/12. 
// Copyright (c) 2012 __MyCompanyName__. All rights reserved. 
// 

#import "AppDelegate.h" 

@implementation AppDelegate 

@synthesize window = _window; 
@synthesize viewController; 
@synthesize navController; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary  *)launchOptions 
{ 
    // Override point for customization after application launch. 

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone"  bundle:nil]; 
    navController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; 
    [self.window addSubview:[navController view]]; 
    [self.window makeKeyAndVisible]; 

    return YES; 
} 

- (void)applicationWillResignActive:(UIApplication *)application 
{ 
} 

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
} 

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
} 

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
} 

- (void)applicationWillTerminate:(UIApplication *)application 
{ 
} 

@end 

ViewController.h

// 
// ViewController.h 
// Sandbox1 
// 
// Created on 6/29/12. 
// Copyright (c) 2012 __MyCompanyName__. All rights reserved. 
// 

#import <UIKit/UIKit.h> 
#import "CreateViewController.h" 

@interface ViewController : UIViewController { 
    CreateViewController *createViewController; 
} 

- (IBAction)clickButton:(id)sender; 

@end 

ViewController.m

// 
// ViewController.m 
// Sandbox1 
// 
// Created on 6/29/12. 
// Copyright (c) 2012 __MyCompanyName__. All rights reserved. 
// 

#import "ViewController.h" 

@implementation ViewController 

- (IBAction)clickButton:(id)sender{ 
    if(!createViewController){ 
     createViewController = [[CreateViewController alloc] initWithNibName:@"CreateViewController" bundle:nil]; 
    } 
    UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]; 
    self.navigationItem.backBarButtonItem = backBarButtonItem; 
    [self.navigationController pushViewController:createViewController animated:YES]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

@end 

CreateViewController.h

// 
// CreateViewController.h 
// Sandbox1 
// 
// Created bon 6/29/12. 
// Copyright (c) 2012 __MyCompanyName__. All rights reserved. 
// 

#import <UIKit/UIKit.h> 

@interface CreateViewController : UIViewController 

@end 

CreateViewController.m

// 
// CreateViewController.m 
// Sandbox1 
// 
// 

#import "CreateViewController.h" 

@implementation CreateViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

/* 
// Implement loadView to create a view hierarchy programmatically, without using a nib. 
- (void)loadView 
{ 
} 
*/ 

/* 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
} 
*/ 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

@end 
+0

這不是代碼轉儲。 –

回答

0

我覺得按鈕在FirstViewController。如果它實現了 - (IBAction)clickButton並在Interface Builder中編寫代碼並將其連接到底部(如果使用Interface Builder)。在FirstViewController.h中寫入createViewController對象和#import <CreateViewController.h>,並且應該使用UINavigationController

FirstViewController.h

#import "CreateViewController.h" 

@interface FirstViewController : UIViewController{ 

    CreateViewController *createViewController; 
} 
-(IBAction)clickButton:(id)sender; 
@end 

FirstViewController.m,你只需要添加下面的方法

-(IBAction)clickButton:(id)sender{ 

if (!createViewController) { 
       createViewController = [[CreateViewController alloc] initWithNibName:@"CreateViewController" bundle:nil]; 

      } 

      UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]; 
      self.navigationItem.backBarButtonItem = backBarButtonItem; 
      [backBarButtonItem release]; 
      [self.navigationController pushViewController:createViewController animated:YES]; 
} 

AppDelegate.h

#import "FirstViewController.h" 
@interface AppDelegate : UIResponder <UIApplicationDelegate> 

@property (strong, nonatomic) UIWindow *window; 
@property (strong, nonatomic) FirstViewController *viewController; 
@property (nonatomic, retain) UINavigationController *navControl; 
@end 

AppDelegate.m,

@synthesize window; 
@synthesize viewController; 
@synthesize navControl; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease]; 
    navControl = [[UINavigationController alloc] initWithRootViewController:self.viewController]; 
[self.window addSubview:[navControl view]]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 
+0

嗨普拉薩德,thx的幫助。我跟着你的代碼,我在模擬器上運行應用程序,我得到了一個像下面的錯誤... 2012-06-29 15:57:56.444 Sandbox1 [2057:207] ***終止應用程序由於未捕獲的異常'NSInternalInconsistencyException' ,原因:'無法在包中加載NIB:'NSBundle(loaded)' name'ViewController_iPhone'' 我錯過了什麼? –

+0

您是否在.xib文件中將按鈕連接到clickButton方法? –

+0

那麼,我有CTRL鍵,左鍵單擊並從故事板(不xib)拖動到 - (IBAction)clickButton:(id)發件人;在ViewController.h文件中。仍然一樣,我得到了黑屏,然後我得到了SIGABRT錯誤和我上面提到的錯誤。 –

相關問題