【问题标题】:Load Another ViewController In A Button From Main ViewController从主视图控制器的按钮中加载另一个视图控制器
【发布时间】:2015-12-02 12:12:11
【问题描述】:

在问这个问题之前,我当然做了一些研究。我在 google 和 stackoverflow 上找到的链接或主题之一是:

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

就我而言,我的项目中有两个视图控制器,即:

  1. ViewController(ViewController.h 和 ViewController.m)——我的主要 vc

  2. 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


【解决方案1】:

您正在使用正确的控制器进行导航,即 UINavigationController,但要使其导航,我建议您使用情节提要。将您的 UINavigationController 作为 rootviewcontroller 并进一步将任何 UIViewController 作为其 rootviewcontroller,然后在第二个 UIViewController 上导航附加一个 Segue 并为其提供一个标识符,比如“PushToSecond”并在您的按钮方法中执行以下操作:

  • (void)向左导航 {

[self performSegueWithIdentifier:@"PushToSecond" sender:self];

}

希望这会有所帮助

【讨论】:

  • 您好 Abhishek,感谢您对我的回复。但是,我不允许使用情节提要。我需要对我的 UI 进行硬编码。现在没事了。我的任务取得了很大进展,我在这里解决了我的问题。我现在将回答我自己的问题。我只是想问 Abhishek 你在哪里学的,或者我在哪里可以学习 rootviewcontroller 之类的基本术语?谢谢!
  • RayWenderlich 和 AppCoda 提供的教程非常适合让您熟悉所有这些术语。
  • @user5295097 这是斯坦福大学在itunes u上发布的关于ios的objective-c编码的课程,它有点旧,但所有基础知识仍然相同,我认为这是一个很好的开始方式. itunes.apple.com/us/itunes-u/developing-apps-for-ios-hd/…
【解决方案2】:

我建议您查看 Apple 关于 UINavigation 的文档。

documentation

您可以根据需要将任意数量的 viewController 对象添加到导航堆栈中,或者 present 它以防您需要临时显示它。

【讨论】:

  • 好的,凯。感谢您的输入。我会在下班后阅读该文档。
【解决方案3】:

我想回答我自己的问题。

我的主 ViewController 中有两个按钮。
LeftViewController 的 1 个按钮
RightViewController 的 1 个按钮。

这意味着当我单击其中一个按钮时,我需要显示每个新的视图控制器或屏幕。

这是我的代码:

就在两个按钮的事件处理程序(我的术语是否正确?我来自 Windows)内,输入以下内容:

  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(或屏幕)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多