在 iOS 中按下按钮时打开另一个视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6509916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:33:15  来源:igfitidea点击:

Open another view when button is pressed in iOS

iosview

提问by Samrat Mazumdar

I am completely new to iOS development and I want to load another view when a button is pressed in a present view.

我对 iOS 开发完全陌生,我想在当前视图中按下按钮时加载另一个视图。

I have created a view based application and following are the codes:

我创建了一个基于视图的应用程序,以下是代码:

//My file name is poo1

//我的文件名是poo1

//This is poo1ViewController.h file

//这是poo1ViewController.h文件

#import <UIKit/UIKit.h>
@interface poo1ViewController : UIViewController 
{
 IBOutlet UIButton *fl;
}
@property (nonatomic,retain) UIButton *fl;
-(IBAction) ifl:(id)sender;    
@end

//This is poo1ViewController.m file

//这是poo1ViewController.m文件

#import "poo1ViewController.h"
@implementation poo1ViewController
@synthesize fl;
-(IBAction) ifl:(id) sender {
}

In the poo1ViewController.xibI have added a button and linked it with File's Owner using Interface Builder.

poo1ViewController.xib我添加了一个按钮并使用 Interface Builder 将其与 File's Owner 链接。

Now I have added another view to this class called as flip.xiban just added a label inside it.

现在我向这个类添加了另一个视图,称为flip.xib刚刚在其中添加了一个标签。

The thing I want is, when I will press the flbutton, the flbutton will call the method ifland which in return should call the second view, i.e. flip.xib.

我想要的是,当我按下fl按钮时,fl按钮将调用该方法,ifl而哪个应该调用第二个视图,即flip.xib.

How will I do that, how can I make the iflmethod to load flip.xibview ?

我将如何做到这一点,如何制作ifl加载flip.xib视图的方法?

回答by bdparrish

UIViewController* flipViewController = [[UIViewController alloc] initWithNibName:@"flip.xib" bundle:[NSBundle mainBundle]];
[self.view addSubview:flipViewController.view];

EDIT:

编辑:

UIViewController* flipViewController = [[UIViewController alloc] initWithNibName:@"flip" bundle:[NSBundle mainBundle]];
[self.view addSubview:flipViewController.view];

Now click on flip.xib, click on the File's Owner on the left hand side of the design area, and change the class to UIViewController. Now CTRL drag from File's Owner icon to the View icon right below it and select the IBOutlet View.

现在单击flip.xib,单击设计区域左侧的File's Owner,并将类更改为UIViewController。现在按住 CTRL 从 File's Owner 图标拖动到它正下方的 View 图标,然后选择 IBOutlet 视图。