2016-01-21 38 views
0

我可以使用prepareForSegue方法在兩個視圖控制器之間傳遞數據。但是通過這種方式,傳遞的數據不能在第二個視圖控制器的init方法中使用。iOS - 將數據從一個視圖控制器傳遞到另一個視圖控制器的init方法中使用

另外,我正在使用XLForm。因此在init方法內訪問數據是必要的。

任何人都可以幫助我解決這個問題。

這裏的第一個視圖控制器的prepareForSegue方法:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

    if ([[segue identifier] isEqualToString:@"SessionToWorkout"]) 
    { 
     WorkoutsViewController *vc = [segue destinationViewController]; 

     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 

     UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:indexPath]; 
     cellName = selectedCell.textLabel.text; 

     NSString *sectionTitle = [self tableView:self.tableView titleForHeaderInSection:indexPath.section]; 
     sectionName = sectionTitle; 

     vc.sectionName = sectionName; 
     vc.cellName = cellName; 
    } 

} 

而這裏的第二個視圖控制器的initWithCoder方法:

- (instancetype)initWithCoder:(NSCoder *)coder 

    { 
     self = [super initWithCoder:coder]; 
     if (self) { 
      //Retrieve Workouts from DB 
      NSString *day_id; 

      day_id = [[DBHandler database] getDayIdWhere:@[@"day_name"] 
              whereValues:@[cellName]]; 

      workoutsArray = [[DBHandler database] getWorkoutsForDayWhere:@[@"day_id"] 
                  whereValues:@[day_id]]; 

      AppLog(@"Workouts array %@", workoutsArray); 

      [self initializeForm]; 
     } 
     return self; 
    } 

裏面的initWithCoder方法,我需要使用單元名變量的值(已從前一視圖控制器傳遞給此視圖控制器)調用數據庫方法。

任何想法或建議如何做到這一點? 在此先感謝。

+0

您可以使用變量'didSet'觀察員,以初始化數據庫。 –

+0

@MichaëlAzevedo對不起,我不熟悉didSet觀察者。你能進一步解釋嗎? – Isu

+0

我剛剛加了一個答案:) –

回答

1

當一個變量被修改時(在變量初始化時不會被調用),將調用didSet觀察者。初始化時單元名設置數據庫:

斯威夫特:

var cellName : String = "" { 
    didSet { 
    // The cell name has been set, create the database here as in any other function 
    } 
} 

的Objective-C:

它在目標C頗爲相似,但你不沒有didSet觀察員,而是使用自定義設置器。唯一的區別是,因爲它是一個二傳手,你必須設置你的變量

@property(nonatomic, strong) NSString * cellName; 

-(void)setCellName:(NSString *)newValue 
{ 
    // First, set the new value to the variable 
    _cellName = newValue; 

    // The cell name has been set, create the database here 
} 
+0

謝謝。哦,等等,這是迅速的權利?但我使用的是Objective-C:/ – Isu

+0

哦,我的不好,你也可以在Objective C中做。讓我編輯我的答案! –

+0

另一個問題,我在前面的視圖控制器的prepareForSegue方法中設置了這個「cellName」變量的值。所以它已經設置好了。 但是當我試圖在initWithCoder方法內訪問它時,它給了我一個空值。也許是因爲在設置值之前調用了「initWithCoder」方法? 如果是這樣,有什麼辦法可以在調用「initWithCoder」方法時設置此變量的值? – Isu

0
if ([[segue identifier] isEqualToString:@"SessionToWorkout"]) 
{ 
    UITableViewCell *cell = sender; 
    // Get reference to the destination view controller 
    WorkoutsViewController *vc = [segue destinationViewController]; 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
    //you can get cell value in didSelectRowAtIndexPath fun 
    cellName = cell.textLabel.text; 
    NSString *sectionTitle = [self tableView:self.tableView titleForHeaderInSection:indexPath.section]; 
    sectionName = sectionTitle; 
    [vc setSectionName: sectionName]; 
    [vc setCellName: cellName]; 
    //check it have right value 
    NSLog(@"Cell name %@ Section name %@", cellName ,sectionName); 
    //*** in viewControllerB set sectionName, cellName as @property and set it to @synthesize 
} 
相關問題