ios - 以编程方式在两个 UIViewController 之间切换
<p><p>我有两个 xib 文件,每个文件都有自己的覆盖 UIViewController 类的类。我在第一个 xib 文件中有一个按钮,当它被按下时应该带我到新的 UIViewController。我设法以不同的方式实现了这种转变:</p>
<pre><code>UIViewController* secondViewController = [ initWithNibName:@"SecondViewControllerName" bundle:];
;
</code></pre>
<p>或</p>
<pre><code>UIViewController* secondViewController = [ initWithNibName:@"SecondViewControllerName" bundle:];
;
</code></pre>
<p>在第二个 UIViewController xib 文件中,我还有一个按钮可以让我回到第一个 UIViewController。但我不知道如何导航回第一个 UIViewController。</p>
<p>我试过了:</p>
<pre><code>;
</code></pre>
<p>对于导航到第二个 UIViewController 的第二种方式,但我得到 <em>unrecognized selector sent to instance</em> 错误。</p>
<p>非常感谢任何形式的帮助。</p>
<hr/>
<p><strong>[编辑#1:添加源代码]</strong></p>
<p>这是第一个 ViewController .m文件的源代码:</p>
<pre><code>#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
;
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)switchToSecondPage:(id)sender
{
SecondViewController *secondViewController = [ initWithNibName:@"SecondViewController" bundle:nil];
;
;
;
;
;
;
}
@end
</code></pre>
<p>这里是第二个 ViewController .m文件的源代码:</p>
<pre><code>#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = ;
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
;
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
;
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)switchToFirstPage:(id)sender
{
;
;
;
;
;
;
}
@end
</code></pre>
<p>这是我的 AppDelegate.m 文件:</p>
<pre><code>#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize viewController = _viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [ initWithFrame:[ bounds]];
// Override point for customization after application launch.
self.viewController = [ initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
;
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
@end
</code></pre>
<p>整个项目被制作为单 View 应用程序,并针对 XCode 4.3 中的 iOS 5.1。我找到了示例项目,它与我遇到的问题相同。这是该项目的 URL:<a href="http://www.devx.com/wireless/Article/42476/0/page/2" rel="noreferrer noopener nofollow">http://www.devx.com/wireless/Article/42476/0/page/2</a>
它只是为早期的 iOS 版本编写的。有没有人能看出我做错了什么,因为我真的没有想法?</p>
<p>PS:无论是否在代码中使用此动画,都会出现同样的问题,所以动画没有问题,我已经检查过了。</p>
<hr/>
<p><strong>[编辑#2:添加错误信息]</strong></p>
<p>这是错误描述。在返回行的 main 方法中如下所示:</p>
<pre><code>int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass());
}
}
</code></pre>
<p>我收到此错误:</p>
<blockquote>
<p>Thread 1: EXC_BAD_ACCESS (code=1, address=0xc00000008)</p>
</blockquote>
<hr/>
<p><strong>[编辑#3:解决方案]</strong></p>
<p>我要感谢@btype 下面的解决方案。我发现了为什么编辑#2 中的代码不起作用。问题是这样做:</p>
<pre><code>SecondViewController *secondViewController = [ initWithNibName:@"SecondViewController" bundle:nil];
</code></pre>
<p><strong>IBAction 方法的内部!</strong> 看起来 ARC 会在到达范围结束时立即删除我的 UIViewController,这就是为什么我得到有关向已发布实例发送消息的信息的原因。我在 ViewController 类中创建了 SecondViewController 私有(private)字段,之后一切正常。</p>
<p>如果有人认为我的解释是错误的并且知道真正困扰我的是什么,请随时回答这个问题并纠正我。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>编辑代码:</p>
<p>在 AppDelegate.m 中</p>
<pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [ initWithFrame:[ bounds]];
// Override point for customization after application launch.
ViewController *viewController = [ initWithNibName:@"ViewController" bundle:nil];
UINavigationController *navController = [ initWithRootViewController:viewController];
self.window.rootViewController = navController;
;
return YES;
}
</code></pre>
<p>FirstViewController.m</p>
<pre><code>- (IBAction)switchToSecondPage:(id)sender
{
;
}
</code></pre>
<p>SeconViewController.m</p>
<pre><code>- (IBAction)switchToFirstPage:(id)sender
{
;
}
</code></pre>
<p>这应该会有所帮助。</p></p>
<p style="font-size: 20px;">关于ios - 以编程方式在两个 UIViewController 之间切换,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/11969998/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/11969998/
</a>
</p>
页:
[1]