【发布时间】:2015-12-02 12:12:11
【问题描述】:
在问这个问题之前,我当然做了一些研究。我在 google 和 stackoverflow 上找到的链接或主题之一是:
How do I load another view controller from the current view controller's implementation file?
就我而言,我的项目中有两个视图控制器,即:
ViewController(ViewController.h 和 ViewController.m)——我的主要 vc
LeftViewController(LeftViewController.h 和 LeftViewController.m) - 我的第二个 vc。它将在我的主 vc 中单击按钮时加载。
请注意,这是我工作的第三天,自我训练,与其他人相比,我发现 Objective-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];
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];
}
对不起,如果这个问题太长了,我不能向办公室的开发人员提问,因为我很害羞,他们不想被打扰:(
【问题讨论】:
-
我认为您在 LeftViewController.m 中的 viewDidLoad 中的代码应该在
navigateLeft方法内。通过这种更改,假设您的第一个 ViewController 在 NavigationController 内,那么该代码将是正确的,否则您将需要一个“modal segue”。我建议阅读有关导航控制器的工作原理、推送转场和模态转场的信息,以便您更好地理解您在这里所做的事情。 -
" 认为 LeftViewController.m 中 viewDidLoad 中的代码应该在 navigateLeft 方法中"
标签: ios objective-c