2010-06-28 28 views
0

我想在導航欄中顯示圖像,而不是帶有操作的後退按鈕。所以現在我已經使用了圖像視圖並在導航欄中顯示了圖像。現在我想在觸摸或單擊圖像視圖時編寫圖像視圖的操作。是否可以爲圖片視圖編寫動作?如何在iPhone-OS 4.0中觸摸圖像視圖時寫入操作

我目前在iPhone OS 4.0中工作。

請參閱my previous question

這裏是我的代碼:

 UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0,5,40,40)]; 
    [self.navigationController.navigationBar addSubview:view1]; 
    view1.backgroundColor = [UIColor clearColor]; 

    UIImage *img = [UIImage imageNamed:@"Back.png"]; 
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img]; 
    [view1 addSubview:imgView]; 

我怎麼能寫圖像視圖的操作?

回答

2

在iOS 3.2之前,你需要一個UIImageView的子類並實現觸摸處理程序。 或用自定義類型的UIButton替換您的UIImageView。

使用iOS 3.2+,UIGestureRecognizer可以讓您的工作高效完成 - 只需在視圖中添加相應的內容即可。

0

不要直接添加視圖到導航欄,除非您準備在未來的操作系統版本中打破它。

UIButton * b = [UIButton buttonWithType:UIButtonTypeCustom]; 
b.image = [UIImage imageNamed:@"Back.png"]; 
b.frame = (CGRect){{0,0},{40,40}}; 
[b addTarget:self action:@selection(wahatever) forControlEvents:UIControlEventTouchUpInside]; 
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:b] autorelease]; 
相關問題