在视图控制器之间切换错误

发布于 2024-11-09 07:36:51 字数 1492 浏览 0 评论 0原文

我在视图控制器之间切换时遇到了一些问题。

我遵循了此页面上的建议:

视图控制器:如何在视图之间切换以编程方式?

我的设置如下;我有一个闪屏,上面有一个按钮,该按钮加载我的第二个视图控制器。我将IntroductionViewController作为根视图,按钮显示“加载网站”,这个加载网站事件我想切换到WebsiteViewController。

在IntroductionViewController 上,我已将按钮设置为IBOutlet 和IBAction,在Click 上NSLogging 很好,所以我知道该方法正在工作。

我需要的是,当您单击“加载网站”时,它会打开 WebsiteViewController。到目前为止,我所得到的是上述问题的功劳:

IntroductionViewController.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

IntroductionViewController.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]; 没有执行任何操作,但正如我所前面说过,NSLogging 这个功能很好。我不知道如何继续。

I'm having a bit of an issue here switching between my view controllers.

I followed the advice on this page:

View Controllers: How to switch between views programmatically?

My setup is as follows; I have a splash screen with a button on it and that button loads my second view controller. I have IntroductionViewController as the root view and the button says "Load Website", this Load Website event I want to switch to WebsiteViewController.

On IntroductionViewController I have set up the button as an IBOutlet and IBAction which is NSLogging fine on Click so I know the method is working.

What I need is when you click Load Website it opens WebsiteViewController. What I have so far is credit of the question above:

IntroductionViewController.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

IntroductionViewController.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];
    }   
}

The line: [self.view addSubview:websiteViewController.view]; doesn't do anything, but as I've said previously, this function is NSLogging fine. I'm not sure how to proceed with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡笑忘祈一世凡恋 2024-11-16 07:36:51

看起来您没有分配 websiteViewController,如果没有分配它,您就没有它的实例可以使用或引用它,记录它只会返回 null。

如果您没有实现UINavigationController,我也会尝试呈现一个ModalViewController。下面是分配 webview 控制器并将其呈现为 modalView 的示例:

-(IBAction)loadWebsite:(id)sender{
    WebsiteViewController *webView = [[WebsiteViewController alloc] init];
    [self.view presentModalViewController:webView animated:YES];
    [webView release];
}

默认过渡样式是 UIModalTransitionStyleCoverVertical 但请查看 文档 了解更多信息。您可以像这样设置过渡样式:

webView.modalTransitionStyle = UIModalTransitionStyleCoverVertical

Looks like you are not allocating your websiteViewController, without allocating it you have no instance of it to use or reference to it, logging it will simply return null.

I would also try presenting is a ModalViewController if you are not implementing UINavigationController. Here is an example of allocating your webview controller and presenting it as a modalView:

-(IBAction)loadWebsite:(id)sender{
    WebsiteViewController *webView = [[WebsiteViewController alloc] init];
    [self.view presentModalViewController:webView animated:YES];
    [webView release];
}

The default transition style is UIModalTransitionStyleCoverVertical but check out the documentation for more. You can set the transition style like so:

webView.modalTransitionStyle = UIModalTransitionStyleCoverVertical
软糯酥胸 2024-11-16 07:36:51

您可能忘记将 IBOutlet 绑定到 WebsiteViewController。对于两种方向支持,您都可以使用

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOr‌​ientation 
 { 
      // Overriden to allow any orientation. 
      return ((interfaceOrientation==UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation==UIInterfaceOrientationLandscapeRight)); 
 }

对方向问题的另一种思考,您可以在显示时将 Frame 设置为您的视图,就像

 - [yourViewController.view setFrame:CGRectMake(0,0,1024,768)] //iPad and 
 - [yourViewController.view setFrame:CGRectMake(0,0,480,320)] //iPhone.

可能这样有效。

塔克斯。

Possible you may have forgot to bind the IBOutlet to your WebsiteViewController. And for both Orientation support you can use

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOr‌​ientation 
 { 
      // Overriden to allow any orientation. 
      return ((interfaceOrientation==UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation==UIInterfaceOrientationLandscapeRight)); 
 }

One more thought for your orientation problem, you can setFrame to your view while displaying like

 - [yourViewController.view setFrame:CGRectMake(0,0,1024,768)] //iPad and 
 - [yourViewController.view setFrame:CGRectMake(0,0,480,320)] //iPhone.

May be this works.

Thax.

玩物 2024-11-16 07:36:51

我敢打赌你的 WebsiteViewController 对象还没有被实例化。 NSLog(@"%@", websiteViewController); 我打赌它是空的! :p 您需要创建对象 - 我不知道如何使用界面生成器来执行此操作。

无论如何,您正在使用旧的方法在视图控制器之间切换。新的方法/最好的方法是使用导航控制器来做到这一点,如下所示:

-(IBAction)loadWebsite:(id)sender{
    [self.navigationController pushViewController:websiteViewController animated:YES];  
}

但是除非您设置了导航控制器,否则这将不起作用,我不知道如何在界面生成器中执行此操作。这就是为什么我讨厌界面生成器并相信你应该停止学习它! :p

如果没有界面生成器,这就是您应该做的:

在您的应用程序委托 .h 文件中将其添加到顶部:

#include "IntroductionViewController.h"

然后将其与代码中的其他 @propertys 一起包含:

@property (nonatomic, retain) UINavigationController *navController;

现在在您的应用程序委托 .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;
}

这将为您提供一个导航控制器(您可以轻松隐藏标题栏... - 请参阅评论)。并显示第一个视图控制器(IntroductionViewController)。

现在运行我上面给你的代码就可以了。 [self.navigationController PushViewController:websiteViewControlleranimated:YES];。这将 viewController 提供给 navigationController,而 navigationController 会自行完成所有添加和删除视图等操作!

而且您根本不必使用界面生成器! :p(好吧......现在您必须学习如何在不拖放的情况下构建视图!)。

无论如何...我希望有所帮助。

I bet your WebsiteViewController object hasn't been instantiated. NSLog(@"%@", websiteViewController); I bet its null! :p You need to create the object - I don't know how to do this using interface builder.

Anyway, you're using the old way to switch between view controllers. The new way/ best way is to use a navigation controller to do it like this:

-(IBAction)loadWebsite:(id)sender{
    [self.navigationController pushViewController:websiteViewController animated:YES];  
}

But this won't work unless you've set up a navigation controller, which I don't know how to do in interface builder. This is why I hate interface builder and believe that you should stop learning with it! :p

Without interface builder this is what you should do:

In your app delegate .h file add this at the top:

#include "IntroductionViewController.h"

Then include this with the other @propertys in the code:

@property (nonatomic, retain) UINavigationController *navController;

Now in your app delegate .m file swap the application did finish launching with options with this:

- (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;
}

That will give you a navigation controller (you can hide the title bar easily... - see the comments). And show the first view controller (IntroductionViewController).

Now running the code I gave you above will work. The [self.navigationController pushViewController:websiteViewController animated:YES];. This gives the viewController to the navigationController and the navigationController does all the adding and removing views and such itself!

And you haven't had to use interface builder at all! :p (well... now you have to learn how to build views without dragging and dropping!).

Anyway... i hope that helped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文