2011-12-12 86 views
0

在我的代碼中,我定製了initWithNibName來接收它需要做的事情。下面的代碼:從推送的視圖控制器中返回一個值

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil theArray:(NSArray *)theDataArray theVal:(NSInteger)theDataValue bRange:(BOOL)isRange bColor:(BOOL)isColor{ 

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
     self.data = theDataArray; 
     self.selectedValue = theDataValue; 
     NSLog(@"NCG - initWithNibName setting selectedValue = %d.", self.selectedValue); 
     //self.originalValue = theDataValue; 
     self.isTheRange = isRange; 
     self.isTheColor = isColor; 
     self.selectedIndex = [self indexFromValue:theDataValue bRange:self.isTheRange bColor:self.isTheColor]; 
     self.doneClicked = NO; 
     //NSLog(@"Selected Row = %d.", self.selectedIndex); 
    } 
    return self; 
} 

當這個觀點上運行,它更新self.selectedValue到一個新的價值。在視圖彈出後,我在視圖中需要這個值。

我該如何獲取這些數據?

回答

1

當我有這個,我通常有一個 'parentReference' 數據成員 例如:

#import "A.h"; 
#import "B.h"; 

      @implementation A 

       - (void) f 
       { 
        B *bInstance = [[B alloc] init]; 
        bInstance.parentRef = self; 

        [self.navigationController pushViewController: bInstance]; 
       } 

//而 'B' 類被聲明爲像

@class A; 
@interface B : UIViewController 
{ 
    A *parentRef; 
} 
@property (nonatomic, assign) A *parentRef; 

這種方式,你可以去回到'召喚'你的班級,做任何你想做的事

(你可能會認爲[self.view superview] == parentRef,但事實並非如此)

+0

這讓我糾正了我遇到的問題,所以我爲它+1了,並將其標記爲答案。我的問題是,我通過initWithNibname傳入的值對於推送新視圖的函數是本地的。一旦我將它們全局化爲調用視圖,一切都奏效了。感謝您指點我正確的方向。 – NCGrimbo

+0

我很高興能夠提供幫助,因爲我自己從這個網站學到了很多東西。祝你好運! – codrut

相關問題