顯示UIActionSheet對象時,調用self出現的警告

編程實現一個Action Sheet: 在XXXViewController.m 中通過執行代碼


UIActionSheet* actionSheet = [[UIActionSheetallocinitWithTitle:@"Are you sure?"delegate:selfcancelButtonTitle:@"No Way!"destructiveButtonTitle:@"Yes, I'm sure"otherButtonTitles:nil];

創建一個UIActionSheet對象。參數delegate爲self爲的是讓ViewController響應用戶交戶信息,調用方法actionSheet: didDismissWithButtonIndex:。編譯後運行成功。但始終在delegate:self上有類型不匹配的警告:Sending 'XXXViewController*' to parameter of incompatible type 'id<UIActionSheetDelegate>'。
initWithTitle:delegate:cancelButtonTitle:destructiveButtonTitle:otherButtonTitles:方法原型如下:
- (id)initWithTitle:(NSString *)title delegate:(id < UIActionSheetDelegate >)delegate cancelButtonTitle:(NSString*)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...

[font=]所以貌似我不能把XXXViewController*類型的self當作參數傳進去。如果消除這個警告是不是應該獲得我這應用程序的delegate傳進去?怎麼獲得這個正確的delegate呢?


解決:

你在XXXViewController後面加上<UIActionSheetDelegate>就可以了,在頭文件的XXXViewController後面
@interface XXXViewController : UIViewController <UIActionSheetDelegate>,看看還有嗎?


果然是這個問題啊!感謝!
UIActionSheetDelegate定義了實現UIActionSheet對象的方法。這個delegate實現了按鈕和其他自定義操作。
如果要爲按鈕增設UIActionSheet操作,那麼就要實現一個遵守此協議的delegate,作爲響應actionsheet的委派。
如果在actionsheet中添加了按鈕,就還要必須實現 actionSheet:clickedButtonAtIndex: 方法響應按鈕按下動作,否則按鈕按下就什麼也不做。
另外還可以實現 actionSheetCancel:方法處理點擊actionsheet取消按鈕的情況。

發佈了23 篇原創文章 · 獲贊 0 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章