単一のビューを持つシンプルなアプリがあり、その上にボタンがあります。ボタンをクリックすると、2 つ目のビューが追加されます。この 2 番目のビューには、1 つのアイコンが付いたシンプルなツールバーがありUIBarButtonItem
ます。私のView Controllerのメッセージを起動するように登録されています。
しかし、ボタンをクリックするとすぐにアプリがクラッシュします。Zombies を有効にすると、View Controller が起動したことがわかります。dealloc
への呼び出しを使用して関数を追加するNSLog()
と、ビューが表示されるとすぐに、ビュー コントローラーが閉じられることがわかります。
shouldAutorotateToInterfaceOrientation
また、トリガーされるようなメッセージはありません。
私の ViewController .h :
#import <UIKit/UIKit.h>
@interface IssueViewController : UIViewController
{
IBOutlet UIBarButtonItem *button;
}
@property (nonatomic, readonly) UIBarButtonItem *button;
- (IBAction)buttonTapped:(id)sender;
+ (void)showSelfInView:(UIView *)view;
@end
その .m :
#import "IssueViewController.h"
@interface IssueViewController ()
@end
@implementation IssueViewController
@synthesize button;
- (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.
self.button.target = self;
self.button.action = @selector(buttonTapped:);
}
- (void)viewDidUnload
{
NSLog(@"unloaded");
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc
{
NSLog(@"Got dealloc");
}
+ (void)showSelfInView:(UIView *)view
{
IssueViewController *ivc = [[IssueViewController alloc] init];
[view addSubview:ivc.view];
}
- (IBAction)buttonTapped:(id)sender
{
[self.view removeFromSuperview];
}
@end
2 番目のビューの表示をトリガーするために使用されるコード:
[IssueViewController showSelfInView:self.view];
誰が私が間違っているのか知っていますか? UIViewController
少なくともビューが削除されるまで保持されないのはなぜですか?
編集
私はARC、強い参照と弱い参照について知っています...非ARCコードでは、でshowSelfInView
:ビューコントローラーを保持し、で自動解放しbuttonTapped
ます。
私にとって、それはそれを達成するための良い方法です。そして、ARCで何か不足しているかどうか、またはビュー/ビューコントローラーの使用方法で何か不足しているかどうかを知りたいです。ビューはまだ表示されているので、私には、そのviewControllerは解放されるべきではありません。ビューコントローラーへの独自の強力な参照を作成する以外に、それを防ぐ方法はありますか?
言い換えた
ビューが表示から削除されるまで、ビューコントローラーを割り当てたままにするパッチのない汚れのない方法はありますか。ビューコントローラーからそれ自体へのポインターはダーティと見なしますが、それが現在使用している方法です。