2012-07-10 85 views
0

我真的很努力,將變量從一個類傳遞到另一個類。我在this問題中回答了問題,並能夠成功實現第一部分(傳遞數據轉發)。我的問題是,當我到達第二部分(傳回數據)時,我掛斷了4),我需要爲second.h文件聲明一個接口。我已經在該.h文件中聲明瞭一個接口:多個接口聲明在一個文件中

@interface RootViewController : UITableViewController <UISplitViewControllerDelegate>{ 

而當我試圖聲明另一個時,我遇到了很多錯誤。我讀了這是可能的,我只是不確定我猜的確切語法。

下面是完整的.h文件中

#import <UIKit/UIKit.h> 


@protocol SubstitutableDetailViewController 
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem; 
- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem; 
@end 


@interface RootViewController : UITableViewController <UISplitViewControllerDelegate>{ 

    UISplitViewController *splitViewController; 

    UIPopoverController *popoverController;  
    UIBarButtonItem *rootPopoverButtonItem; 

    NSMutableArray *logMessages; 
} 

@property (nonatomic, assign) IBOutlet UISplitViewController *splitViewController; 

@property (nonatomic, retain) UIPopoverController *popoverController; 
@property (nonatomic, retain) UIBarButtonItem *rootPopoverButtonItem; 


@end 

回答

2

要添加第二個(或第三或第四)接口,只要把它用逗號

實例你說什麼?我想我可以做一個。

@interface ViewController : UIViewController <DateControllerDelegate, TimeControllerDelegate, DivisionControllerDelegate, FormControllerDelegate, MemoryControllerDelegate, UIAlertViewDelegate, UITextFieldDelegate, UIActionSheetDelegate, UITableViewDelegate, UITableViewDataSource, NSFetchedResultsControllerDelegate, MFMailComposeViewControllerDelegate> { 

看起來你需要查看委託方法。這是我能想到的最簡單的解釋。

Delegates 

//In parent .m file: 
//assign the delegate 
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if([segue.identifier isEqualToString:@"segueName"]) 
    { 
     childController *foo = segue.destinationViewController; 
     foo.delegate = self; 
    } 

} 

//implement protocol method(s): 
- (void) methodName:(dataType*) dataName 
{ 
    //An example of what you could do if your data was an NSDate 
    buttonLabel.titleLabel.text = [[date description] substringToIndex:10]; 
} 

//In parent .h file: 
//import child header 
#import "ChildName.h" 

//indicate conformity with protocol 
@interface ParentName : UIViewController <ChildNameDelegate> 

//In child .h file 
//declare protocol 
@protocol ChildNameDelegate 
- (void) methodName:(dataType*) dataName; 
@end 

//declare delegate 
@property (unsafe_unretained, nonatomic) id<ChildNameDelegate> delegate; 


//In child .m file 
//synthesize delegate 
@synthesize delegate; 

//use method 
- (IBAction)actionName:(id)sender 
{ 
    [delegate methodName:assignedData]; 
} 

而這裏的有關委託一個很好的答案這可能比我更好的方式: How do I create delegates in Objective-C?

+0

啊啊啊謝謝!我知道我一定是做錯了什麼,除了用逗號分隔接口之外,我嘗試了所有的東西。haha – BloonsTowerDefence 2012-07-10 17:10:10

+0

他們是協議!不是接口!不要稱他們:) – Mario 2012-07-10 17:35:18