2011-03-28 47 views
0

我正在Xcode中製作應用程序,並遇到一些問題。我使用GameKit框架來允許兩個iOS設備之間的藍牙通信。應用程序的設置使得其中一個設備是「主」設備,另一個是「從設備」,根據從「主」設備接收的數據更改其屏幕內容。用戶可以選擇是主設備還是從設備,當做出選擇時,另一臺設備自動成爲相反的角色。這全部在一個視圖控制器類中完成。選擇角色時,子視圖將添加到baseViewController。引用superview的方法

我的問題是,當添加子視圖時,我希望能夠使用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"]; 
} 

該代碼的絕大部分是標準的蘋果文檔/例子的一部分,但我將它包括在內以理解邏輯流程。

我認爲問題源於becomeMaster:sendersetBaseViewController:bvc方法。誰能幫助解決?非常感謝!

+0

還有一個SlaveViewController子視圖。此外,沒有編譯時錯誤(我相信我設置了實例變量並將它們正確地合成)。 – 2011-03-28 01:50:35

回答

0

你得到了什麼樣的崩潰? EXC_BAD_ACCESS?嘗試在您的可執行文件的參數中打開NSZombieEnabled。這是很難說這可能是導致飛機墜毀,但你可以嘗試改變你的setBaseViewController:實現這樣的:

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

並添加[baseViewController release];MasterViewController-dealloc方法。

請記住,並非完全有必要爲baseViewController設置自定義設置器。如果你有下列財產申報在你的頭文件:

@property (nonatomic, retain) BaseViewController *baseViewController; 

你使用@synthesize baseViewController,因爲你已經產生-setBaseViewController:方法,用鍵 - 值觀察內置支持如果你不熟悉的目的。 -C 2.0屬性,我建議閱讀Apple's documentation

+0

我對內存管理有點不好意思,所以這裏希望這聽起來不笨。你是不是要說''bvc release]'添加到'-dealloc'方法?因爲從我坐的位置看,bvc看起來像是+1,並且看到'baseViewController'是一個ivar,我已經在'-dealloc'中釋放了它,這意味着它將處於-1或當你考慮autorelease時可能還有其他的東西。對困惑感到抱歉。 – 2011-03-28 05:47:54

+0

非常感謝!它像一個魅力一樣工作!但是,我想確定我沒有在某處存儲內存。只是爲了檢查,這是我的masterViewController類所具有的:在.m文件中合成的已聲明的ivar baseViewController,與發佈時完全一樣的setBaseViewController方法,以及只有一個版本的baseViewController的-dealloc方法。它是否正確? – 2011-03-28 05:54:30

+0

不用擔心。請記住,所有這些變量實際上都是_pointers_,而且'-dealloc'方法不能訪問名爲'bvc'的_variable_,而是'baseViewController'引用的對象,我假設它是一個實例變量'MasterViewController'。當你設置'baseViewController = [bvc retain];'時,你將_object_的保留計數增加1,並設置_variable_'baseViewController'指向那個對象。因此釋放'baseViewController'是正確的。 – 2011-03-28 05:55:05