0

xcode でシングル ビュー アプリケーション テンプレートを使用しています。最初のビュー コントローラーを作成し、新しい .m と .h と xib を使用して別のビュー コントローラーを追加しました。

ボタン IBAction をクリックして 2 番目のビューに移動できますが、「戻る」ボタンに使用しているコードでは最初のビューに戻らず、すべてがクラッシュします。私が使用していたチュートリアルに従っていると思われるコードを含めました。さらに、ボタンをクリックして制御し、.h の IBAction に行をドラッグして、secondViewController ボタンにフックしました。これは、最初のビュー コントローラーで行ったことであり、そこで動作するようです。

誰かがそれを助けることができれば、それは素晴らしいことです!

    //from my first view controller .h which works
-(IBAction) buttonPressedPayTable: (id) sender; 

//from my first view controller.m which also works and gets me to the second view
-(IBAction) buttonPressedPayTable: (id) sender
{
SecondViewController *payTableView = [[SecondViewController alloc]
initWithNibName:@"SecondViewController" bundle:nil];
    [self.view addSubview:payTableView.view];
}


//from my second view controller .h that will not get me back to the first view without crashing

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController
{

}
-(IBAction) back: (id)sender;

@end

//from my second view controller .m which doesn't seem to work
#import "SecondViewController.h"


@implementation SecondViewController

-(IBAction) back: (id)sender

{
    [self.view removeFromSuperview];
}




@end
4

6 に答える 6

1

UINavigation コントローラーを使用する

[self.navigationController popViewControllerAnimated:YES];
于 2012-10-26T09:06:44.423 に答える
0

ナビゲーション方式を使用して、1つのViewControllerから別のViewControllerに切り替えることができます。

ビューコントローラに関するアップルのドキュメントを参照してください

于 2012-10-26T09:17:07.560 に答える
0

モーダル ビューを使用する方がよい場合があります。したがって、 addSubView の代わりに次を使用します。

[payTableView setModalPresentationStyle:UIModalPresentationFullScreen];
[payTableView setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:payTableView animated:YES];

次に、seconViewController の back メソッドで:

[self dismissModalViewControllerAnimated:YES];

ModalTransitionStyle を Apple が提供するものに変更できます:D

お役に立てれば

于 2012-10-26T09:19:43.533 に答える
0

ここでデリゲートを使用する必要があります。

最初のビューの .h でメンバー変数を宣言します。

@interface FirstViewControllerClassName: UIViewController
    {
    SecondViewController *payTableView;
    }
    @property (nonatomic, retain) SecondViewController *payTableView;

.m :

@synthesize payTableView;

-(IBAction) buttonPressedPayTable: (id) sender
{
if (!payTableView)
     payTableView = [[SecondViewController alloc]
initWithNibName:@"SecondViewController" bundle:nil];
payTableView.delegate = self;
payTableView.isFinishedSelector = @selector(removeView);
    [self.view addSubview:payTableView.view];
}

- (void)removeView
{
   [payTableView.view removeFromSuperview];
   [payTableView release];
   payTableView = nil;
}


//Just declare a member variable delegate in secondviewcontroller like:

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController
{
   id delegate;
   SEL isFinishedSelector;
}

@property (nonatomic, assign) id delegate;
@property (nonatomic, assign) SEL isFinishedSelector;


-(IBAction) back: (id)sender;

@end

#import "SecondViewController.h"


@implementation SecondViewController
@synthesize delegate;
@synthesize isFinishedSelector;

-(IBAction) back: (id)sender

{
         if ([self.delegate respondsToSelector:isFinishedSelector])
                [self.delegate performSelector:isFinishedSelector];
}




@end
于 2012-10-26T09:45:29.970 に答える
0

[payTableView.view removeFromSuperview];

ビュー全体を削除していると思います。それがアプリがクラッシュする理由です

于 2012-10-26T09:48:04.483 に答える
0

秒をのViewControllerとして追加しないでください。 a を使用してビュースタックに新しいものを追加します 次に、ナビゲーションコントローラーの戻るボタンを使用するか、コードで独自のボタンを使用できますsubviewview
UINavigationControllerUIViewController[self.navigationController pushViewController:payTableView animated:YES];[self.navigationController popViewControllerAnimated:YES];

または、ストーリーボードを使用して単純なセグエを使用することもできます。

于 2012-10-26T09:22:10.780 に答える