Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 通过按按钮以编程方式打开现有视图_Ios_Objective C - Fatal编程技术网

Ios 通过按按钮以编程方式打开现有视图

Ios 通过按按钮以编程方式打开现有视图,ios,objective-c,Ios,Objective C,我是iOS开发的新手,现在面临着一项对我来说似乎微不足道的任务:当按下view controller 1中的按钮时打开view controller 2。没有涉及故事板和xib文件,因此我通过网络搜索找到的方法,如initWithNibName、segue e.a.的用法不适用 更新: 仍在为这个问题挣扎。当按下ViewController1中的按钮切换到ViewController2时,应用程序中将打开一个空白(白色)视图。 同时,我切换到使用故事板,因为我需要导航控制。但仍然没有使用xib文

我是iOS开发的新手,现在面临着一项对我来说似乎微不足道的任务:当按下view controller 1中的按钮时打开view controller 2。没有涉及故事板和xib文件,因此我通过网络搜索找到的方法,如initWithNibName、segue e.a.的用法不适用

更新:

仍在为这个问题挣扎。当按下ViewController1中的按钮切换到ViewController2时,应用程序中将打开一个空白(白色)视图。 同时,我切换到使用故事板,因为我需要导航控制。但仍然没有使用xib文件

下面是ViewController1.m中的相关代码:

#import "ViewController2.h"

@interface ViewController1 ()

@property (strong, nonatomic) IBOutlet UIButton *GoToViewController2;

@end

@implementation ViewController1

- (IBAction)GoToViewController2:(id)sender
{
    ViewController2* vc2 = [[ViewController2 alloc] init];
    [self.navigationController pushViewController:vc2 animated:YES];
    NSLog(@"Went to ViewController2");
}

要在现有视图控制器的基础上以模式显示新的视图控制器,必须先分配并初始化viewController2,然后从viewController1显示它:

VC2 *vc2 = [[VC2 alloc] init];
//Do initialization ViewController2 stuff here, if needed
....................
//Then present it from your ViewController1
[self presentViewController:vc2 animated:YES completion:nil];

在按钮的方法中

SomeViewController *someVC = [[SomeViewController alloc] init];
[self.navigationController pushViewController:someVC animated:YES];

这将在默认的navigationController堆栈上推送新的视图控制器,并设置过渡动画

使用此链接这是u的帮助,如果您没有navigationController?则使用
[自我呈现视图控制器:动画:完成:],但对于大多数情况,导航控制器是管理视图控制器堆栈的最佳方式。特别是当提问者说他们是iOS开发的新手时。如果没有故事板,而他问这个问题,我真的不认为他有导航控制器需要快速回复。尝试在新设置的测试项目中测试此问题,但此处出现错误:“GRViewController”没有可见的@interface声明选择器“presentViewController:animated:”,并在GRViewController.m:-(iAction)GoToviewController 2:(id)发送方{GRViewController2*grVC2=[[GRViewController2 alloc]init]中使用此代码;[self-presentViewController:grVC2 animated:YES];}抱歉,看起来我忘记了什么:代码已完成-(iAction)GoToviewController 2:(id)发送方{GRViewController2*grVC2=[[GRViewController2 alloc]init];[self-presentViewController:grVC2 animated:YES completion:nil];}错误消失。