![](/img/trans.png)
[英]How can i Load my main ViewController's view, from another viewcontroller's icon action?
[英]Load Another ViewController In A Button From Main ViewController
在问这个问题之前,我当然做了一些研究。 我在Google和stackoverflow上找到的链接或主题之一是:
就我而言,我的项目中有两个视图控制器:
ViewController(ViewController.h和ViewController.m)-我的主要vc
LeftViewController(LeftViewController.h和LeftViewController.m)-我的第二个vc。 单击我的主vc中的按钮后,它将被加载。
请注意,这是我工作,自我训练的第三天,与C#等其他人相比,我发现Objective-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];
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<---
}
C。 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];
}
对不起,如果这个问题太长了,我不能向办公室里的开发人员射击我的问题,因为我很害羞,他们也不想被打扰:(
您正在使用正确的Controller进行导航,即UINavigationController,但要使其进行导航,我建议您使用情节提要。 将您的UINavigationController用作rootviewcontroller,再将任何UIViewController用作其rootviewcontroller,然后在第二个UIViewController上导航,将Segue附加到它并为其提供标识符,让我们说“ PushToSecond”,然后在您的按钮方法中执行以下操作:
[self performSegueWithIdentifier:@“ PushToSecond”发送者:自我];
}
希望这会有所帮助
我想回答我自己的问题。
我的主ViewController中有两个按钮。
1个用于LeftViewController的按钮
1个RightViewController按钮。
这意味着当我单击其中的一个按钮时,需要显示每个新的视图控制器或屏幕。
这是我的代码:
在两个按钮的事件处理程序(我的术语正确吗?我来自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(或屏幕)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.