0

我真的很努力将变量从一个类传递到另一个类。我正在关注这个问题的答案,并且能够成功实施第一部分(传递数据转发)。我的问题是当我进入第二部分(传回数据)时,我对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
4

1 回答 1

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];
}

这里有一个很好的关于委托的答案,可能比我的要好得多: 如何在 Objective-C 中创建委托?

于 2012-07-10T17:01:58.323 回答