2010-07-16 56 views
4

我有一個簡單的項目來呈現一個模態視圖控制器並根據被按下的模態VC中的哪個按鈕傳回一個字符串。我全部基於觀看iTunes U上的斯坦福大學課程。它看起來像我有一切正確,但我得到了一些編譯器警告。實現模態視圖控制器數據傳輸的委託方法

首先,我得到一個在TransferViewController.m

稱爲第二passing argument 1 of 'setDelegate:' from incompatible pointer type我送四個警告,叫Invalid receiver type 'id <MyModalViewControllerDelegate>*'但這些都不是在構建結果區MyModalViewController.m顯示,而旁邊的違規線,這兩條線在每個按鈕操作。

下面的代碼...

// TransferViewController.h 

#import <UIKit/UIKit.h> 
#import "MyModalViewController.h"; 

@interface TransferViewController : UIViewController <MyModalViewControllerDelegate> { 
    UILabel *label; 
    UIButton *button; 
} 

@property (nonatomic, retain) IBOutlet UILabel *label; 
@property (nonatomic, retain) UIButton *button; 

- (IBAction)updateText; 

@end 

// TransferViewController.m 

#import "TransferViewController.h" 

@implementation TransferViewController 

@synthesize label; 
@synthesize button; 

- (IBAction)updateText { 
    MyModalViewController *myModalViewController = [[MyModalViewController alloc] init]; 
    myModalViewController.delegate = self; // I get the warning here. 
    [self presentModalViewController:myModalViewController animated:YES]; 
    [myModalViewController release]; 
} 

- (void)myModalViewController:(MyModalViewController *)controller didFinishSelecting:(NSString *)selectedDog { 
    label.text = selectedDog; 
    [self dismissModalViewControllerAnimated:YES]; 
} 

@end 

// MyModalViewController.h 

#import <UIKit/UIKit.h> 

@protocol MyModalViewControllerDelegate; 

@interface MyModalViewController : UIViewController { 
    UIButton *abby; 
    UIButton *zion; 
    id <MyModalViewControllerDelegate> delegate; 
} 

@property (assign) id <MyModalViewControllerDelegate> delegate; 

- (IBAction)selectedAbby; 
- (IBAction)selectedZion; 

@end 

@protocol MyModalViewControllerDelegate <NSObject> 

@optional 

- (void)myModalViewController:(MyModalViewController *)controller didFinishSelecting:(NSString *)selectedDog; 

@end 

// MyModalViewController.m 

#import "MyModalViewController.h" 

@implementation MyModalViewController 

@synthesize delegate; 

- (IBAction)selectedAbby { 
    if ([self.delegate respondsToSelector:@selector (myModalViewController:didFinishSelecting:)]) { 
     [self.delegate myModalViewController:self didFinishSelecting:@"Abby"]; 
    } 
} 

- (IBAction)selectedZion { 
    if ([self.delegate respondsToSelector:@selector (myModalViewController:didFinishSelecting:)]) { 
     [self.delegate myModalViewController:self didFinishSelecting:@"Zion"]; 
    } 

} 

回答

4

獲取id <something>和BEF後襬脫那些* S的礦石delegate

所以,再一次讓這個

id <MyModalViewControllerDelegate> *delegate; 

id <MyModalViewControllerDelegate> delegate; 
+0

謝謝!當我說我理解使用委託的概念時,我想我在另一個問題上撒了謊,但我認爲我現在就這樣做了。當我在父VC中定義委託方法時,由於它在父代中實現,所以它可以訪問父代中的ivars。當模態VC使用該方法時,它會獲得回到父級的管道。 – Steve 2010-07-16 17:16:08

+0

關於無效類型問題,我認爲這是Objective-C如此寬容的情況之一。很高興讓我錯誤地將委託定義爲一個指針,但是當我真的試圖以這種方式使用它時,我感到很沮喪 - 關於如何定義它的提示在編譯器警告中會很好。也許在xCode 4! – Steve 2010-07-16 17:16:49

+0

是的,看起來像你得到它:) 一個委託仍然指向一個對象,所以如果你會說SomeDelegateClass *委託,你仍然需要*,事情是這個ID已經有這個*'內'它,所以你不需要明確寫它。 – 2010-07-16 21:07:23

相關問題