BloonsTowerDefence
BloonsTowerDefence

Reputation: 1204

Multiple Interface declarations in one file

I am really struggling with with passing variables from one class to another. I was following the answer in this question, and was able to implement the first part (Passing Data Forward) successfully. My problem is when I get to the second part (Passing Data Back), I get hung up on 4) where I need to declare an interface for my second.h file. I already have an interface declared in that .h file:

@interface RootViewController : UITableViewController <UISplitViewControllerDelegate>{

And when I try to declare another one, I run into lots of errors. I read that it is possible, I am just not sure the exact syntax I guess.

Here's the complete .h file

#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

Upvotes: 0

Views: 1558

Answers (1)

Dustin
Dustin

Reputation: 6803

To add a second (or third or fourth) interface, just put it in with a comma

Example you say? I suppose I could do one.

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

It looks like you need to look up the delegate method. Here's the simplest explanation of it that I could come up with.

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

And here's a good answer about delegates that's probably way better than mine: How do I create delegates in Objective-C?

Upvotes: 2

Related Questions