,我當然做了一些研究。我有對谷歌和計算器找到的鏈接或主題是這樣的:在提問這個問題之前,先從主ViewController加載另一個ViewController
How do I load another view controller from the current view controller's implementation file?
在我的情況,我在我的項目,即兩個viewcontrollers:
視圖控制器(視圖控制器.h和ViewController.m) - 我的主vc
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];
}
對不起如果這個問題太長了,我不能向辦公室的開發人員拍我的問題,因爲我很害羞,他們不想被打擾:(
我認爲你的viewDidLoad在LeftViewController.m中的代碼應該在'navigateLeft'方法中。假如你的第一個ViewController在NavigationController裏面,那麼這個代碼將會是正確的,否則你需要一個「模態segue」。我推薦閱讀關於導航控制器如何工作,推動賽道和模態賽段,以便更好地展現你在這裏做的事情。 –
「認爲在你的viewDidLoad在LeftViewController.m中的代碼應該在navigateLeft方法中」<---確切!在我看到您的評論之前,我做了同樣的事情。它的工作原理!謝謝馬克! – user5295097