2013-12-23 166 views
25

問題導航控制器推送 - 視圖 - 控制器

如何從一個視圖控制器導航到另一個簡單地用一個按鈕的觸摸的內心活動?

更多信息

我在一個示例項目試了一下,臺階是:

  1. 創建示例單一視圖的應用程序。

  2. 添加一個新文件 - > Objective-C Class with XIB for user interface(ViewController2)。

  3. 將一個按鈕添加到ViewController.xib並控制單擊按鈕到ViewController.h以創建觸摸內部事件。

  4. 轉到ViewController.m新發IBAction爲它改成這樣......

    - (IBAction)GoToNext:(id)sender 
    { 
        ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil]; 
    
        [[self navigationController] pushViewController:vc2 animated:YES]; 
    } 
    

代碼運行沒有錯誤,我的NSLog測試按鈕的功能。但它仍然不會導航到第二個視圖控制器。任何幫助,將不勝感激。

+0

您是否真正創建了導航控制器並將其添加到UI?你的'[self navigationController]'引用是否回到nil? – ColinE

回答

46

Swift3

**Push** 

做像

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let vc = storyboard.instantiateViewControllerWithIdentifier("NewsDetailsVCID") as NewsDetailsViewController 
vc.newsObj = newsObj 
navigationController?.pushViewController(vc, 
animated: true) 

或更安全

if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsDetailsVCID") as? NewsDetailsViewController { 
     viewController.newsObj = newsObj 
     if let navigator = navigationController { 
      navigator.pushViewController(viewController, animated: true) 
     } 
    } 

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
    let vc = self.storyboard?.instantiateViewControllerWithIdentifier("NewsDetailsVCID") as! NewsDetailsViewController 
     vc.newsObj = newsObj 
      present(vc!, animated: true, completion: nil) 

或更安全的

if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsDetailsVCID") as? NewsDetailsViewController 
    { 

    vc.newsObj = newsObj 
    present(vc, animated: true, completion: nil) 
    } 





//Appdelegate.m 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" 
                 bundle:nil]; 
    UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:self.viewController]; 
    self.window.rootViewController = navigation; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 


//ViewController.m 

- (IBAction)GoToNext:(id)sender 
{ 
    ViewController2 *vc2 = [[ViewController2 alloc] init];  
    [self.navigationController pushViewController:vc2 animated:YES]; 
} 

迅速

//Appdelegate.swift 

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { 
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds) 

    let navigat = UINavigationController() 
    let vcw = ViewController(nibName: "ViewController", bundle: nil) 

    // Push the vcw to the navigat 
    navigat.pushViewController(vcw, animated: false) 

    // Set the window’s root view controller 
    self.window!.rootViewController = navigat 

    // Present the window 
    self.window!.makeKeyAndVisible() 
    return true 
} 

//ViewController.swift 

@IBAction func GoToNext(sender : AnyObject) 
{ 
    let ViewController2 = ViewController2(nibName: "ViewController2", bundle: nil) 
    self.navigationController.pushViewController(ViewController2, animated: true) 
} 
+0

謝謝,我不知道爲什麼我不記得之前做過這一步,但它肯定對我有用。 –

+1

嗯,好,每次都不要忘記,在應用程序代理中執行此操作,k –

+0

我使用appdelegate通過推視圖控制器,但後視圖controllerin swift 3.0 –

-1

首先創建導航控制器並將其作爲窗口對象的rootViewController提供。

0

的UINavigationController沒有在UIViewController中自動呈現。

這是你應該在Interface Builder中看到的。文件所有者具有查看導航控制器的出口,並且從導航控制器出口到實際視圖;

Interface Builder

+0

也許我曾經創建自動具有導航控制器的項目。如果是這樣,那麼創建項目的方式是什麼,以便我可以找到默認的導航控制器。 –

+0

打開你的ViewController的筆尖,按照我的說明改變它。 –

2

對於斯威夫特使用下面的代碼:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { 
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds) 
    self.window!.backgroundColor = UIColor.whiteColor() 

    // Create a nav/vc pair using the custom ViewController class 

    let nav = UINavigationController() 
    let vc = NextViewController(nibName: "NextViewController", bundle: nil) 

    // Push the vc onto the nav 
    nav.pushViewController(vc, animated: false) 

    // Set the window’s root view controller 
    self.window!.rootViewController = nav 

    // Present the window 
    self.window!.makeKeyAndVisible() 
    return true 

} 

的ViewController:

@IBAction func Next(sender : AnyObject) 
{ 
    let nextViewController = DurationDel(nibName: "DurationDel", bundle: nil) 

    self.navigationController.pushViewController(nextViewController, animated: true) 
} 
9
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"storyBoardName" bundle:nil]; 
MemberDetailsViewController* controller = [storyboard instantiateViewControllerWithIdentifier:@"viewControllerIdentiferInStoryBoard"]; 

[self.navigationController pushViewController:viewControllerName animated:YES]; 
0

這是工作完美:

PD:記住導入目的地VC:

#import "DestinationVCName.h" 

    - (IBAction)NameOfTheAction:(id)sender 
{ 
     DestinationVCName *destinationvcname = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationVCName"]; 
    [self presentViewController:destinationvcname animated:YES completion:nil]; 
} 
0
- (void) loginButton:(FBSDKLoginButton *)loginButton 
didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result 
       error:(NSError *)error{ 
    UINavigationController *nav = [self.storyboard instantiateViewControllerWithIdentifier:@"nav"]; 
    ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"LoggedInVC"]; 
    [nav pushViewController:vc animated:YES]; 
    [self presentViewController:nav animated:YES completion:nil]; 
} 

「NAV」是故事板的ID爲我的導航控制器 「VC」是故事板的ID爲連接到我的導航控制器我的第一視圖控制器

-hope這有助於

0

的AppDelegate到的ViewController:

let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
let loginPageView = mainStoryboard.instantiateViewControllerWithIdentifier("leadBidderPagerID") as! LeadBidderPage 
var rootViewController = self.window!.rootViewController as! UINavigationController 
rootViewController.pushViewController(loginPageView, animated: true) 

ViewControllers之間:

let loginPageView = self.storyboard?.instantiateViewControllerWithIdentifier("scoutPageID") as! ScoutPage 
self.navigationController?.pushViewController(loginPageView, animated: true) 
0

如果您使用的斯威夫特:

let controller = self.storyboard!.instantiateViewControllerWithIdentifier("controllerID") 
self.navigationController!.pushViewController(controller, animated: true) 
1

使用此代碼瀏覽下一個視圖 - 控制,如果你使用的分鏡方式遵循這個下面的代碼,

UIStoryboard *board; 

if (!self.storyboard) 
{ 
    board = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
} 
else 
{ 
    board = self.storyboard; 
} 

ViewController *View = [board instantiateViewControllerWithIdentifier:@"yourstoryboardname"]; 
[self.navigationController pushViewController:View animated:YES]; 
+0

謝謝工作! :) –

2

在你的按鈕操作中使用這個代碼(Swift 3.0.1):

let vc = self.storyboard?.instantiateViewController(
    withIdentifier: "YourSecondVCIdentifier") as! SecondVC 

navigationController?.pushViewController(vc, animated: true) 
+0

@EricAya我對這個stackOverFlow非常新,所以剛剛發佈了一些我看到並申請了自己的東西:) –

+0

@EricAya好吧,先生 –

0
UIViewController *vc=[self.storyboard instantiateViewControllerWithIdentifier:@"storyboardId"]; 
[self.navigationController pushViewController:vc animated:YES]; 
相關問題