首先我想說我對ipad/ipod/iphone開發非常陌生,對於objective-c也是如此。這就是說,我試圖開發一個針對iPad的小應用程序,使用Xcode和IB,基本上,我有一個表,爲表中的每個UITableViewCell添加一個按鈕,該按鈕包含一個包含一個圖像。如何從UITableViewCell.accessoryView調用PopOver控制器?
下面是代碼:
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的按鈕一酥料餅的控制出現。
我想這對中的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];
這段代碼的問題是,它顯示了酥料餅在應用程序中查看的頂部,在調試時,我看到的值「矩形」,他們分別是:
x = 267
y = 13
所以我認爲這是很明顯的原因正在顯示酥料餅這麼漲的看法,所以我的問題是,我怎麼能得到正確的價值觀爲酥料餅剛剛出現低於ce的附件視圖上的按鈕二?另外,正如你所看到的,我告訴它使用「cell.accessoryView」作爲「inView:」屬性,這樣好嗎?
千恩萬謝,說完全成功了! – Vic 2010-05-13 14:17:08