2012-02-22 8 views
0

UIPopoverControllerトラブル

//.h 
@interface LoginViewController : UIViewController <UIPopoverControllerDelegate> 
.... 
@end 

//.m 
- (IBAction)popoverTest:(id)sender 
{ 
    UIViewController *popoverContent = [[UIViewController alloc] init]; 
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)]; 

    UILabel *nameLabel = [[UILabel alloc] init]; //edited, fixed UILabel allocation 
    nameLabel.text = @"Person's name"; 
    [nameLabel sizeToFit]; 
    nameLabel.frame = CGRectMake(10, 10, nameLabel.frame.size.width, nameLabel.frame.size.height); 
    [myView addSubview:nameLabel]; 

    popoverContent.view = myView; 

    popoverContent.contentSizeForViewInPopover = CGSizeMake(300, 300); 

    UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent]; 
    popoverController.delegate = self; 

    [popoverController presentPopoverFromRect:((UIButton *)sender).frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 

    NSLog(@"ran all code"); 
} 

を私はUIViewを作成し、サブビューとしてラベルを配置し、UIViewController.viewに私の見解を割り当てます。それから、Popover Controllerを作成し、Popover Controllerのサイズを決め、デリゲートを設定してボタンのフレームから提示しました。

私はSIGABRTとアプリのクラッシュを受けます。

紛失しているものがありますか?

編集:私はUILabel割り当てを修正しました。問題はいつもそこにある。

+0

あなたのIBAction内にブレークポイントを置き、クラッシュする箇所を確認してください。また、[例外]を有効にしてください(http://stackoverflow.com/questions/4961770/run-stop-on-objective-c-exception-in-xcode-4) –

答えて

3

あなたのコードはかなり奇妙です。例えば

は、なぜあなたはビューを作成するのですか、あなたはそれがあなたのポップオーバーのコンテンツビューに追加するには?

その後、あなたはメモリリークに注意をしなければなりません。あなたのコードにはたくさんのものがあります。表示UIPopoverControllerUIViewcontrollerのための簡単な例ここでは、言った

self.popoverが保持ポリシーと@propertyある

- (IBAction)yourAction:(id)sender 

    UIButton* senderButton = (UIButton*)sender; 

    UIViewController* yourController = [[UIViewController alloc] init]; 

    UIPopoverController* pop = [[UIPopoverController alloc] initWithContentViewController:yourController]; 
    pop.delegate = self; 
    pop.popoverContentSize = CGSizeMake(300, 300);  

    self.popover = pop; 

    [pop presentPopoverFromRect:senderButton.bounds inView:senderButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
    pop.passthroughViews = nil; 

    [yourController release]; 
    [pop release]; 
} 

。この方法で、UIPopoverControllerDelegateのメソッド(またはどこでも)で、あなたのポップオーバーを解放したり、それを却下することができます。

希望します。

P.S.手書きで書いたのでコードを確認してください。

編集

あなたはポップオーバーを作成する際に通常、そのコンテンツビューコントローラであるか、またはカスタムUIViewControllerまたはUINavigationController(このケースでは、あなたがそのナビゲーションバーを活用したいです)。

たとえば、代わりにシンプルUIViewControllerの、あなたは1

//.h 
@interface CustomUIViewController : UIViewController 

@end 

//.m 
@implementation CustomUIViewController 

// other code here... 

- (void)viewDidLoad 
{ 
    // here you are sure that the view has been loaded in memory (alternatively override loadView method), so 
    UIView* greenView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)]; 
    green.backgroundColor = [UIColor greenColor]; 
    [self.view addSubview:greenView]; 
    [greenView release]; 
} 

@end 

カスタムを作成することもできますし、AAポップオーバーの中にそれを使用

- (IBAction)yourAction:(id)sender 

    UIButton* senderButton = (UIButton*)sender; 

    CustomUIViewController* yourController = [[CustomUIViewController alloc] init]; 

    // same as before... 
} 
+0

提案してくれてありがとう、私はそれを考慮します。私はこのプロジェクトでARCを使用していると述べたはずです。 – Padin215

+0

あなたの答えを読んでください。あなたは私のUIPopoverControllerを問題を修正したインスタンス変数にすることを提案しました – Padin215

+0

あなたは大歓迎です。 –

0

問題は、次のようだ:

UILabel *nameLabel; 
nameLabel.text = @"Person's name"; 

nameLabelが初期化されていません。ランダムなメモリを指していて、テキストプロパティで-setText:を呼び出すと、アプリケーションがクラッシュします。これとは別に

、あなたはいくつかのメモリリークを持っています。注意:allocに電話する場合は、releaseどこかにある必要があります。 (私は新しい自動alloc/releaseでこれがどのように機能するか分かりません)。

+0

私はそれを "UILabel * nameLabel = [[UILabel alloc] init]; "それを修正しませんでした。 – Padin215

1

は私の問題from this question.

短い答えを解きます: ARCはUIPopoverControllerを保持しません。

私はそれをインスタンス変数にして正常に動作します。