简体   繁体   English

引用superview的方法

[英]Referencing superview's methods

I'm making an application in Xcode, and running into some problems. 我正在Xcode中创建一个应用程序,并且遇到了一些问题。 I'm using the GameKit framework to allow for bluetooth communication between two iOS devices. 我使用GameKit框架允许两个iOS设备之间的蓝牙通信。 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. 选择角色后,会将子视图添加到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. 我的问题是,当添加子视图时,我希望能够使用baseViewController类中的方法发送数据。 With the current setup, the device invoking the action becomeMaster:sender crashes. 使用当前设置,调用该操作的设备becomeMaster:sender崩溃。

What I've tried so far is, 到目前为止,我尝试过的是

BaseViewController: 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: 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. 大部分代码是标准Apple文档/示例的一部分,但我将其包括在内是为了理解逻辑流程。

I believe the problem originates to with the becomeMaster:sender and setBaseViewController:bvc methods. 我相信问题出在与becomeMaster:sendersetBaseViewController:bvc方法有关。 Could anyone help fix? 谁能帮忙解决? Thanks so much! 非常感谢!

What kind of crash are you getting? 您会发生哪种崩溃? EXC_BAD_ACCESS ? EXC_BAD_ACCESS Try turning on NSZombieEnabled in your executable's arguments. 尝试在可执行文件的参数中打开NSZombieEnabled It's difficult to say what could be causing the crash, but you might try changing your setBaseViewController: implementation to this: 很难说是什么导致崩溃的原因,但是您可以尝试将setBaseViewController:实现更改为此:

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

And add [baseViewController release]; 并添加[baseViewController release]; to MasterViewController 's -dealloc method. MasterViewController-dealloc方法。

Keep in mind that it's not entirely necessary to have a custom setter for baseViewController . 请记住,这不是完全必要有一个定制的setter 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 . 然后,您使用@synthesize baseViewController ,已经为您生成了-setBaseViewController:方法,并内置了键值观察支持。如果您不熟悉Objective-C 2.0属性,建议阅读Apple的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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