[英]Switching between View Controllers Error
我在我的视图控制器之间切换时遇到了一些问题。
我遵循了此页面上的建议:
我的设置如下; 我有一个带有按钮的初始屏幕,该按钮加载我的第二个视图 controller。 我有 IntroductionViewController 作为根视图,按钮显示“加载网站”,我想切换到 WebsiteViewController 这个加载网站事件。
在 IntroductionViewController 上,我已将按钮设置为 IBOutlet 和 IBAction,单击时 NSLogging 很好,所以我知道该方法有效。
我需要的是,当您单击加载网站时,它会打开 WebsiteViewController。 到目前为止,我所拥有的是上述问题的功劳:
简介ViewController.h
#import <UIKit/UIKit.h>
#import "WebsiteViewController.h"
@class WebsiteViewController;
@interface IntroductionViewController : UIViewController {
IBOutlet UIButton *websiteButton;
IBOutlet WebsiteViewController *websiteViewController;
}
@property (nonatomic, retain) UIButton *websiteButton;
@property (nonatomic, retain) WebsiteViewController *websiteViewController;
-(IBAction)loadWebsite:(id)sender;
@end
简介ViewController.m
#import "IntroductionViewController.h"
@implementation IntroductionViewController
@synthesize websiteButton;
@synthesize websiteViewController;
-(IBAction)loadWebsite:(id)sender{
if(self.websiteViewController.view.superview == nil)
{
[self.view addSubview:websiteViewController.view];
}
}
行: [self.view addSubview:websiteViewController.view];
没有做任何事情,但正如我之前所说,这个 function 是 NSLogging 很好。 我不确定如何进行此操作。
看起来你没有分配你的 websiteViewController,如果没有分配它,你就没有它的实例来使用或引用它,记录它只会返回 null。
如果您没有实现UINavigationController
ModalViewController
这是分配 webview controller 并将其呈现为 modalView 的示例:
-(IBAction)loadWebsite:(id)sender{
WebsiteViewController *webView = [[WebsiteViewController alloc] init];
[self.view presentModalViewController:webView animated:YES];
[webView release];
}
默认的过渡样式是UIModalTransitionStyleCoverVertical
但请查看文档了解更多信息。 您可以像这样设置过渡样式:
webView.modalTransitionStyle = UIModalTransitionStyleCoverVertical
我敢打赌您的 WebsiteViewController object 尚未实例化。 NSLog(@"%@", websiteViewController);
我敢打赌它的 null:.p 你需要创建 object - 我不知道如何使用界面生成器来做到这一点。
无论如何,您正在使用旧方法在视图控制器之间切换。 新方法/最佳方法是使用导航 controller 这样做:
-(IBAction)loadWebsite:(id)sender{
[self.navigationController pushViewController:websiteViewController animated:YES];
}
但这将不起作用,除非您设置了导航 controller,我不知道如何在界面生成器中执行此操作。 这就是为什么我讨厌界面生成器并相信你应该停止学习它::p
如果没有界面生成器,您应该这样做:
在您的应用程序 delegate.h 文件中在顶部添加:
#include "IntroductionViewController.h"
然后将其与代码中的其他 @propertys 一起包含:
@property (nonatomic, retain) UINavigationController *navController;
现在在您的应用程序 delegate.m 文件交换中,应用程序确实使用以下选项完成启动:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
// lets make the navigation controller by setting up a view controller first.
IntroductionViewController *viewController = [[IntroductionViewController alloc] initWithNibName:nil bundle:nil];
viewController.title = @"Home";
navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[navController setNavigationBarHidden:NO]; // set to yes to hide the title bar
[viewController release];
[self.window addSubview:navController.view];
return YES;
}
这将为您提供导航 controller (您可以轻松隐藏标题栏... - 请参阅评论)。 并显示第一个视图 controller (IntroductionViewController)。
现在运行我上面给你的代码就可以了。 [self.navigationController pushViewController:websiteViewController animated:YES];
. 这将 viewController 提供给了 navigationController,navigationController 完成了所有添加和删除视图等操作!
而且您根本不必使用界面构建器:.p(嗯...现在您必须学习如何构建视图而无需拖放!)。
无论如何......我希望这有帮助。
您可能忘记将 IBOutlet 绑定到您的 WebsiteViewController。 对于两个方向支持,您可以使用
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Overriden to allow any orientation.
return ((interfaceOrientation==UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation==UIInterfaceOrientationLandscapeRight));
}
再考虑一下您的方向问题,您可以在显示时将框架设置为您的视图
- [yourViewController.view setFrame:CGRectMake(0,0,1024,768)] //iPad and
- [yourViewController.view setFrame:CGRectMake(0,0,480,320)] //iPhone.
可能是这个作品。
萨克斯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.