How do I load another view controller from the current view controller's implementation file?
就我而言,我的项目中有两个viewcontrollers,即:
ViewController(ViewController.h和ViewController.m) - 我的主要vc
LeftViewController(LeftViewController.h和LeftViewController.m) - 我的第二个vc。点击主vc上的按钮即可加载。
请注意,这是我工作的第3天,自我训练,与其他人相比,我觉得客观c很难,比如C#......哦,我想念C#。
继续,我尝试按照我所获得的链接中的步骤以及我在顶部给出的链接。
以下是我的代码:
一个。在我的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];
湾我的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。
一个。 LeftViewController.m中的viewDidLoad
- (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];
}
很抱歉,如果这个问题太长,我无法向办公室的开发人员提出问题,因为我很害羞而且他们不想被打扰:(
答案 0 :(得分:1)
您正在使用正确的控制器进行导航,即UINavigationController,但为了使其导航,我建议您使用故事板。将您的UINavigationController作为rootviewcontroller,然后将任何UIViewController作为其rootviewcontroller,然后在第二个UIViewController上导航将Segue连接到它并为其提供一个标识符,例如“PushToSecond”,并在您的按钮中执行以下操作:
[self performSegueWithIdentifier:@“PushToSecond”sender:self];
}
希望这会有所帮助
答案 1 :(得分:1)
答案 2 :(得分:0)
我想回答我自己的问题。
我的主ViewController中有两个按钮
LeftViewController的1个按钮
RightViewController的1个按钮。
含义当我点击其中一个按钮时,我需要显示每个新的视图控制器或屏幕。
以下是我的代码:
在两个按钮的事件处理程序(我的术语是正确的吗?我来自Windows)中,放入以下内容:
“左侧屏幕/视图控制器”的代码
- (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];}
同样,我需要输入相同类型的代码(只需将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(或屏幕)。