15

ビューコントローラー内にプログラムで作成したボタンがあります。ボタンが押されたら、メソッドを使用してプログラムでポップオーバーを作成する必要があります。

私のView Controller.mのViewDidLoadで作成されたボタン

 UIView *moreFundInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 540, 620)];
[self.view addSubview:moreFundInfoView];
[moreFundInfoView setBackgroundColor:[UIColor RMBColor:@"b"]];

btnContact = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];


[btnContact setFrame:CGRectMake(390, 575, contactButton.width, contactButton.height)];
 btnContact.hidden = NO;
[btnContact setTitle:@"Contact" forState:(UIControlStateNormal)];
[moreFundInfoView addSubview:btnContact];

[btnContact addTarget:self action:@selector(showContactDetails:) forControlEvents:UIControlEventTouchUpInside];

次に、ボタンが押されたときに使用するメソッドがあります。

-(void) showContactDetails: (id) sender
{
UIViewController *popoverContent = [[UIViewController alloc]init];

UIView *popoverView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 300)];

[popoverView setBackgroundColor:[UIColor RMBColor:@"b"]];

popoverContent.view = popoverView;

popoverContent.contentSizeForViewInPopover = CGSizeMake(200, 300);

UIPopoverController *contactPopover =[[UIPopoverController alloc] initWithContentViewController:popoverContent];

[contactPopover presentPopoverFromRect:btnContact.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES ];

[contactPopover setDelegate:self];

}

ここで何が欠けていますか?正常に動作しますが、ボタンをクリックするとすぐにアプリがクラッシュします。デリゲートの問題だと思いますが、よくわかりません。アドバイスをいただければ幸いです。

4

4 に答える 4

33

このコードが役立つと思います。あなたは確かにデリゲートメソッドがありません

ViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navigationController];
popover.delegate = self;
popover.popoverContentSize = CGSizeMake(644, 425); //your custom size.
[popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections: UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionUp animated:YES];

UIPopover Delegate メソッドを忘れていないことを確認してください。そうしないと、アプリケーションが確実にクラッシュします。それが必須です。

于 2013-02-28T06:47:42.477 に答える
2
 UIViewController *controller = [[UIViewController alloc] init];
 [view removeFromSuperview]; //view is a view which is displayed in a popover
 controller.view = view;
 UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:controller];
 popover.delegate = self;
[popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
于 2014-12-30T16:55:56.567 に答える
1

はい、「retain」のプロパティを「strong」に変更すると、ピッカー ビュー オブジェクトを保持できます。あなたのコードの問題は、メソッドが終了すると UIPopoverController オブジェクトが自動的に割り当て解除されることだったと思います。強力なプロパティを作成すると、オブジェクトを強く指すようになります。

于 2014-01-13T11:34:31.973 に答える
1

私がしなければならなかったのは、.h ファイルでプロパティを「retain」から「strong」に変更することだけでした。それが機能し、アプリがクラッシュするのを防ぎました。

于 2013-02-12T05:26:58.767 に答える