ios 如何在故事板中的 UIViewController 之间手动切换?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8348109/
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
How can I manually switch between UIViewControllers in storyboard?
提问by kokoko
All I need is to view a UIView
controller in same storyboard file manually with code. I use storyboard to make all forms and connections. My application starts in navigation controller, which provides me access to UIView
(LoginViewController
) and then it goes to tab bar controller, which provides 4 UIViews
. According to every UIView
I have .h
and .m
files. I know about segue method, it is simple, but I need manual method. Maybe I am doing something wrong.
我所需要的只是UIView
使用代码手动查看同一个故事板文件中的控制器。我使用故事板来制作所有形式和联系。我的应用程序从导航控制器开始,它为我提供了对UIView
( LoginViewController
) 的访问,然后它转到了标签栏控制器,它提供了 4 UIViews
。根据UIView
我所有的.h
和.m
文件。我知道 segue 方法,它很简单,但我需要手动方法。也许我做错了什么。
I was trying to use this method for pushing view controller in IBAction
:
我试图使用这种方法将视图控制器推入IBAction
:
[self.view pushViewController:LoginViewController animated:YES];
But it makes an error:
但它会出错:
Unexpected interface name ‘LoginViewController': expected expression
意外的接口名称“LoginViewController”:预期的表达
It took a lot of time to figure out what is wrong, but I had not succeed.
Here is my RollEnemyController.m
file:
花了很多时间才弄清楚出了什么问题,但我没有成功。这是我的RollEnemyController.m
文件:
// RollEnemyController.m
#import "RollEnemyController.h"
#import "LoginViewController.h"
@implementation RollEnemyController;
@synthesize AttackButtonPressed;
- (IBAction)AttackButtonPressed:(id)sender {
LoginViewController* controller = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
[self.view pushViewController:controller];
}
@end
And this is header file:
这是头文件:
// RollEnemyController.h
#import <UIKit/UIKit.h>
@interface RollEnemyController : UIViewController
- (IBAction)RollButtonPressed:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *AttackButtonPressed;
@end
回答by matsr
I'm guessing that you are using a UINavigationController
. Then you can simply do like this:
我猜你正在使用UINavigationController
. 然后你可以简单地这样做:
LoginViewController *controller = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
Update:
更新:
If you are using a UIStoryboard
, you can set the identifier of your new viewcontroller, and then push it onto your navigationController. To set the identifier, choose your view, open the Attributes Inspector, and set the identifier ("LoginIdentifier" in my example). Then you can do this:
如果您使用的是UIStoryboard
,您可以设置新视图控制器的标识符,然后将其推送到您的导航控制器上。要设置标识符,请选择您的视图,打开 Attributes Inspector,然后设置标识符(在我的示例中为“LoginIdentifier”)。然后你可以这样做:
LoginViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginIdentifier"];
[self.navigationController pushViewController:controller animated:YES];
As a sidenote, I see that you are using capital characters for your methods. You should probably try to avoid that, and instead use lowered first-characters in your method names. And since you say you are learning Objective-C, you should check out this awesome thread here on SO: link.
作为旁注,我看到您在方法中使用大写字符。您可能应该尽量避免这种情况,而是在方法名称中使用降低的首字符。既然你说你正在学习 Objective-C,你应该在 SO 上查看这个很棒的线程:链接。
Update 2:
更新 2:
Hereis a zip file with a project showing how to do this. :-)
这是一个带有项目的 zip 文件,显示了如何执行此操作。:-)
回答by Danieldms
hello try to use this code
你好尝试使用此代码
Storyboard put ID = "xxx * Name Desire" mark use StoryboarID
Storyboard put ID = "xxx * Name Desire" 标记使用 StoryboarID
UIStoryboard * storyboard = self.storyboard;
DetailViewController * detail = [storyboard instantiateViewControllerWithIdentifier: @ "xxx * Name Desire"];
[self.navigationController pushViewController: detail animated: YES];
回答by sergio
In this statement:
在此声明中:
[self.view pushViewController:LoginViewController animated:YES];
it seems you are trying to push a class. You should push an object, your actual controller:
看来您正在尝试推动课程。您应该推送一个对象,即您的实际控制器:
LoginViewController* controller = [[LoginViewController alloc] init...];
[self.view pushViewController:controller animated:YES];
this will at least compile, and if all the rest is fine, also give you the second controller.
这至少会编译,如果其他一切都很好,还给你第二个控制器。
EDIT:
编辑:
I missed one point. You are pushing the view controller on to a view. That makes no sense, you should push the controller on to the navigation controller:
我错过了一分。您正在将视图控制器推到视图上。这是没有意义的,您应该将控制器推到导航控制器上:
<AppDelegate> *del = (AppDelegate*)[UIApplication sharedApplication].delegate;
[del.navigationController pushViewController:controller animated:YES];
This is true, at least, if you created your project from the Navigation-based template (which creates an application delegate with a reference to the navigation controller). Otherwise, please provide details about how you create the navigation controller.
至少,如果您从基于导航的模板(它创建了一个引用导航控制器的应用程序委托)创建了您的项目,这是正确的。否则,请提供有关如何创建导航控制器的详细信息。
回答by Mark Adams
You mentioned in a comment that you're using UIStoryboard
. Are you aware of UIStoryboardSegue
? All you have to do it control-drag from the button to the next view controller to establish a segue. Then you can choose the type of transition. Be aware that your view controllers need to be part of a UINavigationController
in the storyboard to perform a "Push" animation.
您在评论中提到您正在使用UIStoryboard
. 你知道UIStoryboardSegue
吗?所有你必须做的,从按钮控制拖动到下一个视图控制器来建立一个segue。然后您可以选择过渡类型。请注意,您的视图控制器需要成为UINavigationController
故事板中的一部分才能执行“推送”动画。