[英]Referencing superview's methods
我正在Xcode中创建一个应用程序,并且遇到了一些问题。 我使用GameKit框架允许两个iOS设备之间的蓝牙通信。 已设置该应用程序,以使其中一个设备是“主设备”,另一个设备是“从属设备”,并根据从“主设备”设备接收的数据更改其屏幕内容。 用户可以选择是主机还是从机,并且做出选择后,另一台设备将自动成为相反的角色。 所有这些都在一个视图控制器类中完成。 选择角色后,会将子视图添加到baseViewController。
我的问题是,当添加子视图时,我希望能够使用baseViewController类中的方法发送数据。 使用当前设置,调用该操作的设备becomeMaster:sender
崩溃。
到目前为止,我尝试过的是
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"];
}
大部分代码是标准Apple文档/示例的一部分,但我将其包括在内是为了理解逻辑流程。
我相信问题出在与becomeMaster:sender
和setBaseViewController:bvc
方法有关。 谁能帮忙解决? 非常感谢!
您会发生哪种崩溃? EXC_BAD_ACCESS
? 尝试在可执行文件的参数中打开NSZombieEnabled
。 很难说是什么导致崩溃的原因,但是您可以尝试将setBaseViewController:
实现更改为此:
-(void)setBaseViewController:(BaseViewController *)bvc {
[self willChangeValueForKey:@"baseViewController"];
[baseViewController autorelease]
baseViewController = [bvc retain];
[self didChangeValueForKey:@"baseViewController"];
}
并添加[baseViewController release];
到MasterViewController
的-dealloc
方法。
请记住,这不是完全必要有一个定制的setter baseViewController
。 如果你在你的头文件中的下列财产申报:
@property (nonatomic, retain) BaseViewController *baseViewController;
然后,您使用@synthesize baseViewController
,已经为您生成了-setBaseViewController:
方法,并内置了键值观察支持。如果您不熟悉Objective-C 2.0属性,建议阅读Apple的文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.