簡體   English   中英

在視圖控制器之間切換錯誤

[英]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)interfaceOr‌​ientation 
 { 
      // 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM