2015-09-07 64 views
0

,我當然做了一些研究。我有對谷歌和計算器找到的鏈接或主題是這樣的:在提問這個問題之前,先從主ViewController加載另一個ViewController

How do I load another view controller from the current view controller's implementation file?

在我的情況,我在我的項目,即兩個viewcontrollers:

  1. 視圖控制器(視圖控制器.h和ViewController.m) - 我的主vc

  2. LeftViewController(LeftViewController.h和LeftViewController.m) - 我的第二個vc。它會在點擊我主vc中的按鈕時加載。

請大家注意,這是我的第3天在我的工作,自我訓練,我覺得Objective-C的挺難相比其他人,如C#...我好想念C#。

接下來,我試着按照我的鏈接以及我在頂部給出的鏈接進行操作。

這裏是我的代碼:

a。在我ViewController.m中的ViewDidLoad內部

// add navigation button left 
UIButton *leftNavigationButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[leftNavigationButton setFrame:CGRectMake(10, 65, 30, 20)]; 
[leftNavigationButton setTitle:@"<" forState:UIControlStateNormal]; 
[leftNavigationButton addTarget:self action:@selector(navigateLeft) forControlEvents:UIControlEventTouchUpInside]; 
[leftNavigationButton.layer setBorderWidth:1.0f]; 
[leftNavigationButton.layer setBorderColor:[[UIColor whiteColor] CGColor]]; 
[leftNavigationButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
[self.view addSubview:leftNavigationButton]; 

b。該方法我ViewController.m內(當然viewDidLoad中以外:

- (void)navigateLeft 
{ 
// LeftViewController *leftViewController = [[LeftViewController alloc] init]; 
// [leftViewController viewDidLoad]; 
    _leftViewController = [[LeftViewController alloc] initWithNibName:@"UI For LeftButton" bundle:nil]; 
    [self.ViewController: self.leftViewController animated:YES completion:nil]; //Error in this line, spefically in self.ViewController<--- 

} 

ÇViewController.h

// 
// ViewController.h 
// Navigation_Task 
// 
// Created by Glenn on 9/4/15. 
// Copyright (c) 2015 Ayi. All rights reserved. 
// 

#import <UIKit/UIKit.h> 

// for LeftViewController 
@class LeftViewController; 

@interface ViewController : UIViewController 

@property (nonatomic, retain) NSMutableString *resultText; 

// for LeftViewController 
@property (strong, nonatomic)LeftViewController *leftViewController; 

@end 

以及最後,(這是不包括在上述的鏈接),我的用於代碼。加載LeftViewController。

一個。viewDidLoad中在LeftViewController.m

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    LeftViewController *leftViewController = [[LeftViewController alloc] initWithNibName:nil bundle:nil]; 

    // set background and title of the view 
    [leftViewController.view setBackgroundColor:RGB(19,181,234)]; 
    leftViewController.title = @"Left View Controller"; 

    [self.navigationController pushViewController:leftViewController animated:YES]; 
} 

對不起如果這個問題太長了,我不能向辦公室的開發人員拍我的問題,因爲我很害羞,他們不想被打擾:(

+1

我認爲你的viewDidLoad在LeftViewController.m中的代碼應該在'navigateLeft'方法中。假如你的第一個ViewController在NavigationController裏面,那麼這個代碼將會是正確的,否則你需要一個「模態segue」。我推薦閱讀關於導航控制器如何工作,推動賽道和模態賽段,以便更好地展現你在這裏做的事情。 –

+0

「認爲在你的viewDidLoad在LeftViewController.m中的代碼應該在navigateLeft方法中」<---確切!在我看到您的評論之前,我做了同樣的事情。它的工作原理!謝謝馬克! – user5295097

回答

1

你正在使用正確的導航控制器,即UINavigationController ,但要使它導航,我會建議你使用故事板。把你的UINavigationController作爲RootViewController的,並進一步採取任何的UIViewController作爲其RootViewController的,然後在第2的UIViewController來瀏覽Segue公司重視它,併爲其提供一個標識符,讓說「PushToSecond」,並在你的按鈕的方法執行以下操作:

  • (無效)navigateLeft {

[自performSegueWithIdentifier:@ 「PushToSecond」 發件人:自];

}

希望這將有助於

+0

嗨阿布舍克,我很感謝你對我的回覆。但是,我不允許使用Storyboard。我需要硬編碼我的用戶界面。現在沒事了。我的任務取得了很大進展,我在這裏解決了我的問題。我現在會回答我自己的問題。我只想問Abhishek你在哪裏學習或在哪裏學習像rootviewcontroller這樣的基本術語和東西?謝謝! – user5295097

+0

由RayWenderlich和AppCoda提供的教程是很好的一個讓你熟悉所有這些條款。 –

+0

好的謝謝阿布舍克! – user5295097

0

我想回答我的問題。

我在我的主要ViewController中有兩個按鈕。
1個用於LeftViewController的按鈕
1個用於RightViewController的按鈕。

含義我需要顯示每個新的viewcontroller或屏幕,當我點擊其中一個按鈕。

這裏是我的代碼:

就在事件處理中(是我的任期正確的,我從我的窗戶?)兩個按鈕,把下面的:

  1. 守則「左屏幕/視圖控制器」

    - (void)navigateLeft 
    { 
        // LeftViewController *leftViewController = [[LeftViewController alloc] init]; 
        // [leftViewController viewDidLoad]; 
        // _leftViewController = [[LeftViewController alloc] initWithNibName:@"UI For LeftButton" bundle:nil]; 
    
         LeftViewController *leftViewController = [[LeftViewController alloc] initWithNibName:nil bundle:nil]; 
    
         // set background and title of the view 
         [leftViewController.view setBackgroundColor:RGB(19,181,234)]; 
         leftViewController.title = @"Left View Controller"; 
    
         [self.navigationController pushViewController:leftViewController animated:YES];} 
    
  2. 同樣,我需要把相同類型的代碼(只是重命名leftViewController到RightViewController)

    的現在
    - (void)navigateRight 
    { 
        RightViewController *rightViewController = [[RightViewController alloc] initWithNibName:nil bundle:nil]; 
    
        // set background and title of the view 
        [rightViewController.view setBackgroundColor:RGB(19,181,234)]; 
        rightViewController.title = @"Right View Controller"; 
    
        [self.navigationController pushViewController:rightViewController animated:YES]; 
    } 
    

我可以將任何兩個按鈕,並且將顯示一個新的ViewController(或屏幕)。

1

我建議你看看Apple的UINavigation文檔。

documentation

您可以添加儘可能多的viewController對象到導航堆棧在需要或present它櫃面你需要暫時表現出來。

+0

Ok Kaey。感謝您的輸入。我將在辦公時間之後閱讀該文檔。 – user5295097

相關問題