How can I manually switch between UIViewControllers in storyboard?(如何在情节提要中的 UIViewControllers 之间手动切换?)
问题描述
我只需要使用代码手动查看同一故事板文件中的 UIView
控制器.我使用故事板来制作所有形式和连接.我的应用程序从导航控制器开始,它让我可以访问 UIView
(LoginViewController
),然后它进入标签栏控制器,它提供 4 个 UIViews
.根据每个 UIView
我有 .h
和 .m
文件.我知道segue方法,它很简单,但我需要手动方法.也许我做错了什么.
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.
我试图在 IBAction
中使用这种方法推送视图控制器:
I was trying to use this method for pushing view controller in IBAction
:
[self.view pushViewController:LoginViewController animated:YES];
但它会出错:
意外的接口名称LoginViewController":预期的表达式
Unexpected interface name ‘LoginViewController’: expected expression
我花了很长时间才弄清楚出了什么问题,但我没有成功.这是我的 RollEnemyController.m
文件:
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
#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
这是头文件:
// RollEnemyController.h
#import <UIKit/UIKit.h>
@interface RollEnemyController : UIViewController
- (IBAction)RollButtonPressed:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *AttackButtonPressed;
@end
推荐答案
我猜你正在使用 UINavigationController
.然后你可以简单地这样做:
I'm guessing that you are using a UINavigationController
. Then you can simply do like this:
LoginViewController *controller = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
更新:
如果您使用的是 UIStoryboard
,您可以设置新视图控制器的标识符,然后将其推送到您的导航控制器上.要设置标识符,请选择您的视图,打开 Attributes Inspector,然后设置标识符(在我的示例中为LoginIdentifier").然后你可以这样做:
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:
LoginViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginIdentifier"];
[self.navigationController pushViewController:controller animated:YES];
作为旁注,我看到您在方法中使用了大写字符.您可能应该尽量避免这种情况,而是在方法名称中使用降低的首字符.既然你说你正在学习 Objective-C,你应该在这里查看这个很棒的线程:链接.
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.
更新 2:
这里是一个 zip 文件,其中包含一个项目,展示了如何执行此操作.:-)
Here is a zip file with a project showing how to do this. :-)
这篇关于如何在情节提要中的 UIViewControllers 之间手动切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在情节提要中的 UIViewControllers 之间手动切换
- android 4中的android RadioButton问题 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01