iphone - 按下按钮时切换 View

标签 iphone ios

当我点击应用程序中的按钮时,我试图加载一个新 View 。 我得到的错误是 -

View Switcher[6867:207] -[UIView pushViewController:animated:]: unrecognized selector sent to instance 0x6010660

源代码片段是-

-(IBAction) blueButtonPressed:(id)sender{

if(self.yellowViewController == nil){
    YellowViewController *yellowController = [[YellowViewController alloc] initWithNibName:@"YellowView" bundle:nil];
    self.yellowViewController = yellowController;
    //[yellowController release];

    //[self.view addSubview:yellowController.view];
//[self.view pushViewController:self.yellowViewController];
    [self.navigationController pushViewController:self.yellowViewController];
[yellowController release];
}
//[self.navigationController pushViewController:self.yellowViewController animated:YES];
}

这是我正在使用的头文件 -

#import <UIKit/UIKit.h>

@class BlueViewController;
@class YellowViewController;


@interface BlueViewController : UIViewController {  
YellowViewController *yellowViewController;
BlueViewController *blueViewController; 
}
-(IBAction)blueButtonPressed:(id)sender;

@property(retain) YellowViewController *yellowViewController;
@property(retain, nonatomic) BlueViewController *blueViewController;
@end

xcode 项目的链接是 - https://rapidshare.com/files/2403429896/View_Switcher.zip

最佳答案

pushViewController 是 UINavigationController 方法。

[self.navigationController pushViewController:self.yellowViewController animated:YES];

我假设您在 UIViewController 子类中


你能发布一个 Xcode 项目吗?我需要看看这个。


好的,我已经查看了您的代码,问题是您没有任何 navigationController。您的应用程序的结构类似于 View 基础应用程序,而不是导航基础应用程序。
结果是您的 self.navigationController == nil,这就是该调用被忽略的原因。

在您的应用程序委托(delegate)中,您需要一些类似这样的代码

UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:switchViewController];
navCon.navigationBarHidden = YES;
self.window.rootViewController = navCon;
[window makeKeyAndVisible];

在应用程序完成...


在您的代码中,当您单击工具栏中的开关时,它会“正常工作”,因为您使用此代码:

[blueViewController.view removeFromSuperview];
[self.view insertSubview:yellowViewController.view atIndex:0];

而且那个进程中没有navigationController。

关于iphone - 按下按钮时切换 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8024408/

相关文章:

ios - 正在定制 UISearchField 是私有(private)的

ios - 为什么 subview Controller 会占据整个屏幕?

iOS 照片库 - 如何获取真实的文件名?

iphone - 如何判断我们何时与 GameCenter GKMatch session 断开连接?

iphone - 如何在drawRect中用渐变填充路径:?

iphone - 如何重置或清除与 CGContext 关联的剪贴蒙版?

ios - 绘制以画笔形状结尾的线条?

iphone - 如何从 iOS 应用程序发送 POST XML?

iphone - 将 UILabels 从任何位置旋转到直立

ios - SpriteKit中的加速度计不起作用