3

最初に、私は ipad/iPod/iphone の開発と、objective-c の初心者です。

そうは言っても、私は Xcode と IB を使用して、iPad をターゲットとする小さなアプリケーションを開発しようとしています。

コードは次のとおりです。

UIImage *img = [UIImage imageNamed:@"myimage.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, img.size.width, img.size.height);
button.frame = frame;   // match the button's size with the image size

[button setBackgroundImage:img forState:UIControlStateNormal];

// set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;

これまでのところ、問題は、ユーザーがセルのaccessoryViewのボタンをタップしたときにPopOverコントロールが表示されるようにすることです。

tableViewの「accessoryButtonTappedForRowWithIndexPath」でこれを試しました:

UITableViewCell *cell = [myTable cellForRowAtIndexPath:indexPath];
UIButton *button = (UIButton *)cell.accessoryView;

//customViewController is the controller of the view that I want to be displayed by the PopOver controller
customViewController = [[CustomViewController alloc]init];

popOverController = [[UIPopoverController alloc]
initWithContentViewController: customViewController];

popOverController.popoverContentSize = CGSizeMake(147, 122);

CGRect rect = button.frame;

[popOverController presentPopoverFromRect:rect inView:cell.accessoryView
permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

このコードの問題は、アプリケーション ビューの上部にポップオーバーが表示されることです。デバッグ中に「rect」の値が表示され、次のようになります。

x = 267
y = 13

なぜ PopOver がビューに表示されているのかは明らかだと思います。私の質問は、PopOver の正しい値をセルのアクセサリビューのボタンのすぐ下に表示するにはどうすればよいですか?

また、ご覧の通り「inView:」属性に「cell.accessoryView」を使うように言っているのですが、よろしいでしょうか?

4

3 に答える 3

8

rect は に対して相対的であるため、button.bounds代わりに を使用してみてください。button.frameinView

また、セルが画面の下部近くにある場合、矢印を上方向に強制しているため、ポップオーバーが適切なサイズで表示されない場合があることに注意してください。これを手動で処理するか、単に Any を使用する必要があります。

于 2010-05-13T02:26:13.417 に答える
1
[popOverController presentPopoverFromRect:rect inView:cell.accessoryView
permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

inView:cell.accessoryView を inView:cell に変更する

于 2011-07-14T00:42:23.047 に答える