将数据传递回父控制器以进行iOS开发

时间:2013-06-14 09:51:14

标签: ios

我有问题将所选数据显示在我的一行部分中的detaillabeltext中,除了重新加载整个表格视图以外的任何其他方法只重新加载某一行的部分?

// RootViewController.m(父控制器)

-(void) selectedData:(NSString*) text
{
  selectedAbsenceType = text;

  NSLog(@"the absence type select is %@",text);
}

-(void) (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    if (indexPath.section == 0)
    {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
}

NSDictionary *dictionary = [dataArray objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"data"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

if([cellValue isEqual: @"Absence Type"])
{
    cell.detailTextLabel.text = selectedAbsenceType;
}
else if([cellValue isEqual:@"Start Date"])
{
    cell.detailTextLabel.text = selectedDate;
}
return cell;

}

=============================================== ============================================ 我在调用协议方法时遇到问题,它在本声明中提示我一个ARC语义问题

[self.delegate selectedData: (NSString*) [self.absenceTypes objectAtIndex:indexPath.row]];:

// child.h

#import <UIKit/UIKit.h>

@protocol childViewControllerDelegate;

@interface AbsenceTypesViewController : UITableViewController
{
   id<childViewControllerDelegate>delegate;
}
@property (nonatomic,weak) id<childViewControllerDelegate> delegate;
@property NSArray *absenceTypes;
@end

@protocol childViewControllerDelegate <NSObject>
-(void) selectedData:(NSString*) text;
@end

//child.m

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath    *)indexPath
{

NSString *selectedCell = nil;
selectedCell = [self.absenceTypes objectAtIndex:indexPath.row];

[self.delegate selectedData: (NSString*) [self.absenceTypes objectAtIndex:indexPath.row]];

//[self.navigationController popViewControllerAnimated:YES];
NSLog(@"%@", selectedCell);

}

2 个答案:

答案 0 :(得分:0)

删除用户类界面中的{ id<childViewControllerDelegate>delegate; }id<childViewControllerDelegate>delegate;表示仅在释放保持对象时才会释放的强变量。但在财产声明中,你提到代表是弱势财产。因此ARC Semantic会给你警告。你也可以通过像__weak id<childViewControllerDelegate>delegate;

那样明确地声明弱代表来使委托变弱

尝试用此替换.h文件内容。

#import <UIKit/UIKit.h>  
@class AbsenceTypesViewController;

@protocol childViewControllerDelegate <NSObject>
-(void) selectedData:(NSString*) text;
@end

@interface AbsenceTypesViewController : UITableViewController
{
   id<childViewControllerDelegate>delegate;
}
@property (nonatomic,weak) id<childViewControllerDelegate> delegate;
@property NSArray *absenceTypes;
@end

这样你就可以向前宣布上课了。

答案 1 :(得分:0)

 You can save all selected option of second view controller in NSMutable Array and save all components  separated by comma and send this array to your parent controller.

 NSMutableArray *selectedVal =[[NSMutableArray alloc] init];

      FirstViewController *FVC = (FirstViewController*)

         if ([FVC isKindOfClass:[FirstViewController class]])
         {

             [FVC setSelectedOption:[selectedVal componentsJoinedByString:@","]];


         }

         [self.navigationController popViewControllerAnimated:YES];