简体   繁体   中英

Referencing superview's methods

I'm making an application in Xcode, and running into some problems. I'm using the GameKit framework to allow for bluetooth communication between two iOS devices. The application is setup so that one of the devices is the "master" and the other is the "slave," changing it's screen content based on data received from the "master" device. The user can select whether to be the master or the slave, and when that choice is made, the other device automatically becomes the opposite role. This is all done in one view controller class. When a role is chosen, a subview is added to the baseViewController.

What my problem is, is that when the subview that is added, I would like to be able to send data using the methods in the baseViewController class. With the current setup, the device invoking the action becomeMaster:sender crashes.

What I've tried so far is,

BaseViewController:

-(IBAction)becomeMaster:(id)sender {
    [self dataToSend:@"slave"]; //tells peer device to become slave, since this device is master
    masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil];
    [masterViewController setBaseViewController:self];
    [self.view addSubview:masterViewController.view];
}

-(void)dataToSend:(NSString *)direction {   
    //—-convert an NSString object to NSData—-  
    NSData* data;   
    NSString *str = [NSString stringWithString:direction];  
    data = [str dataUsingEncoding: NSASCIIStringEncoding];  
    [self mySendDataToPeers:data];
}

-(void)dataToSend:(NSString *)direction {   
    //—-convert an NSString object to NSData—-  
    NSData* data;   
    NSString *str = [NSString stringWithString:direction];  
    data = [str dataUsingEncoding: NSASCIIStringEncoding];  
    [self mySendDataToPeers:data];
}

 //----------------------------------------------------------------------------//

- (void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context {   
    //—-convert the NSData to NSString—-    
    NSString* str;  
    str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    [self useReceivedData:str];
    [str release];
}

-(void)useReceivedData:(NSString *)str {
    if ([str isEqualToString:@"forward"]) {
        [slaveViewController.view setBackgroundColor:[UIColor blackColor]];
    }
}

MasterViewController:

-(void)setBaseViewController:(BaseViewController *)bvc {
    baseViewController = bvc;
}

-(IBAction)goForward:(id)sender {
    actionLabel.text = @"goingForward";
    [baseViewController dataToSend:@"forward"];
}

Most of that code is part of the standard Apple documentation/examples, but I included it for understanding the flow of logic.

I believe the problem originates to with the becomeMaster:sender and setBaseViewController:bvc methods. Could anyone help fix? Thanks so much!

What kind of crash are you getting? EXC_BAD_ACCESS ? Try turning on NSZombieEnabled in your executable's arguments. It's difficult to say what could be causing the crash, but you might try changing your setBaseViewController: implementation to this:

-(void)setBaseViewController:(BaseViewController *)bvc {
    [self willChangeValueForKey:@"baseViewController"];
    [baseViewController autorelease]
    baseViewController = [bvc retain];
    [self didChangeValueForKey:@"baseViewController"];
}

And add [baseViewController release]; to MasterViewController 's -dealloc method.

Keep in mind that it's not entirely necessary to have a custom setter for baseViewController . If you have the following property declaration in your header file:

@property (nonatomic, retain) BaseViewController *baseViewController;

And you use @synthesize baseViewController , the -setBaseViewController: method is already generated for you, with key-value observing support built in. If you aren't familiar with Objective-C 2.0 properties, I suggest reading Apple's documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM