2

2 つの xib ファイルがあり、それぞれに UIViewController クラスをオーバーライドする独自のクラスがあります。最初のxibファイルにボタンがあり、押されたときに新しいUIViewControllerに移動します。私はさまざまな方法でその移行を行うことができました。

UIViewController* secondViewController = [[UIViewController alloc] initWithNibName:@"SecondViewControllerName" bundle:[NSBundle mainBundle]];
        [self.view addSubview:secondViewController.view];

また

UIViewController* secondViewController = [[UIViewController alloc] initWithNibName:@"SecondViewControllerName" bundle:[NSBundle mainBundle]];
        [self.navigationController pushViewController:secondViewController animated:YES];

2 番目の UIViewController xib ファイルには、最初の UIViewController に戻るボタンも 1 つあります。しかし、最初の UIViewController に戻る方法がわかりません。

私は試した:

[self.navigationController popViewControllerAnimated:YES];

2番目のUIViewControllerに移動する2番目の方法ですが、認識できないセレクターがインスタンスエラーに送信されます。

あらゆる種類の助けを前もって感謝します。


[編集 #1: ソースコードの追加]

最初のView Controller .mファイルのソースコードは次のとおりです。

#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)switchToSecondPage:(id)sender
{
    SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    [UIView beginAnimations:@"flipping view" context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition: UIViewAnimationTransitionCurlDown forView:self.view cache:YES];

    [self.view addSubview:secondViewController.view];

    [UIView commitAnimations];  

}

@end

2 番目のビュー コントローラー .m ファイルのソース コードは次のとおりです。

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super 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
{
    [UIView beginAnimations:@"flipping view" context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationTransition: UIViewAnimationTransitionCurlUp forView:self.view.superview cache:YES];

    [self.view removeFromSuperview];

    [UIView commitAnimations];
}

@end

ここに私の AppDelegate.m ファイルがあります:

#import "AppDelegate.h"

#import "ViewController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
}

- (void)applicationWillTerminate:(UIApplication *)application
{
}

@end

プロジェクト全体がシングル ビュー アプリケーションとして作成され、XCode 4.3 で iOS 5.1 を対象としています。問題を抱えているのと同じことを行うサンプルプロジェクトを見つけました。そのプロジェクトの URL は次のとおりです: http://www.devx.com/wireless/Article/42476/0/page/2 これは、以前の iOS バージョン用に書かれています。私は本当にアイデアがないので、誰かが私が間違っていることを見ることができますか?

PS: このアニメーションをコードで使用しても使用しなくても、同じ問題が発生するため、アニメーションは問題ありません。確認済みです。


[編集 #2: エラー情報の追加]

ここにエラーの説明があります。以下にリストされているように、戻り行のメインメソッドで:

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

次のエラーが表示されます。

スレッド 1: EXC_BAD_ACCESS (コード = 1、アドレス = 0xc00000008)


[編集 #3: 解決策]

以下の解決策について@btypeに感謝します。編集 #2 のコードが機能しない理由がわかりました。問題はこれをしていました:

SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

IBActionメソッドの中身!スコープの終わりに到達するとすぐに ARC が UIViewController をワイプしたように見えます。そのため、リリースされたインスタンスへのメッセージの送信に関する情報を取得していました。ViewController クラスで SecondViewController プライベート フィールドを作成した後、すべてが正常に機能しました。

私の説明が間違っていると思う人がいて、何が本当に私を悩ませているのかを知っている人がいたら、遠慮なくこの質問に答えて、私を正してください。

4

1 に答える 1

4

コードを編集します。

AppDelegate.m で

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
ViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}

FirstViewController.m

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

SeconViewController.m

- (IBAction)switchToFirstPage:(id)sender
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

これは役立つはずです。

于 2012-08-15T13:34:49.737 に答える