xcode 单击按钮加载新视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1871604/
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
Load a new view on click of a button
提问by Pinky
I have placed two button in navigation controller on click of each button it should open new view , how to load a new view on click of a button
我在导航控制器中放置了两个按钮,单击每个按钮应该打开新视图,如何在单击按钮时加载新视图
回答by Pinky
Right-click the Classes group in the project window of Xcode and select Add->New File… Select the UIViewController subclass template from the Cocoa Touch Classes and click Next. Name the view FirstViewController (make sure that the Also Created flag is selected) and click Finish. You get two files (FirstViewController.h and FirstViewController.m). Right-click again the Classes group and choose Add -> New File… This time, select View XIB template from the User Interface under iPhone. Click Next and name the file FirstViewController.xib. Click Finish. Repeat the last paragraph for the second and third view (name them SeconViewController and ThirdViewController).
在 Xcode 的项目窗口中右键单击 Classes 组,然后选择 Add->New File... 从 Cocoa Touch Classes 中选择 UIViewController 子类模板,然后单击 Next。将视图命名为 FirstViewController(确保选中 Also Created 标志)并单击 Finish。您将获得两个文件(FirstViewController.h 和 FirstViewController.m)。再次右键单击 Classes 组并选择 Add -> New File... 这次,从 iPhone 下的 User Interface 中选择 View XIB template。单击下一步并将文件命名为 FirstViewController.xib。单击完成。对第二个和第三个视图重复最后一段(将它们命名为 SeconViewController 和 ThirdViewController)。
Right-click the Classes group in the project window of Xcode and select Add->New File… Select the UIViewController subclass template from the Cocoa Touch Classes and click Next. Name the view FirstViewController (make sure that the Also Created flag is selected) and click Finish. You get two files (FirstViewController.h and FirstViewController.m). Right-click again the Classes group and choose Add -> New File… This time, select View XIB template from the User Interface under iPhone. Click Next and name the file FirstViewController.xib. Click Finish. Repeat the last paragraph for the second and third view (name them SeconViewController and ThirdViewController).
在 Xcode 的项目窗口中右键单击 Classes 组,然后选择 Add->New File... 从 Cocoa Touch Classes 中选择 UIViewController 子类模板,然后单击 Next。将视图命名为 FirstViewController(确保选中 Also Created 标志)并单击 Finish。您将获得两个文件(FirstViewController.h 和 FirstViewController.m)。再次右键单击 Classes 组并选择 Add -> New File... 这次,从 iPhone 下的 User Interface 中选择 View XIB template。单击下一步并将文件命名为 FirstViewController.xib。单击完成。对第二个和第三个视图重复最后一段(将它们命名为 SeconViewController 和 ThirdViewController)。
回答by JonEasy
When you're operating with Navigation Controllers, transferring to a new screen and returning to the previous screen is achieved with pushing and popping new ViewControllers on a stack. So, assuming you have a UITableView inside your Navigation Controller you simply add this line to didSelectRowAtIndexPath:(NSIndexPath *)indexPath:
当您使用导航控制器进行操作时,通过在堆栈上推送和弹出新的 ViewController 来实现转移到新屏幕并返回到前一屏幕。因此,假设您的导航控制器中有一个 UITableView,您只需将此行添加到 didSelectRowAtIndexPath:(NSIndexPath *)indexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyNewController *controller = [MyNewController alloc] init];
[[self navigationController] pushViewController:controller animated:YES];
}