如何将选定的值从第二类发送到iphone中的第一类indexpath.row

时间:2011-09-16 10:12:09

标签: iphone objective-c

 I have two class `first` class and `second` class

I have created 3 section on `tableview` cell for display different value
If I select on section 0 and `indexpath.row` 0 then it push me on `second` class where I have all name list in `tableview` if i select any name then that name should display on first class on section 0  after it happen then i can sent this display value to server please help me on this
what i have to write on second class this method:

当我添加此代码时,我收到此错误 - [airport isEqualToString:]:无法识别的选择器发送到实例0x4e54760 2011-09-16 16:44:37.693 RegexKitLiteDemo [36975:207] * 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [airport isEqualToString:]:无法识别的选择器发送到实例0x4e54760 “

this is my Tfirst.m file



- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"accessory selected");


    if ([indexPath section] == 0)
    {
       // load the appropriate view for the accessory selected
        if ([indexPath row] == 0)
        {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nameControllerDidSelect:) name:NameControllerDidSelectNotification object:viewtwo];

            viewtwo=[[Originstart alloc]initWithNibName:@"Originstart" bundle:nil];
            [self.navigationController pushViewController:viewtwo animated:YES];
            [viewtwo release];
}
        else{
            NSLog(@"accessory right");
            }

        //[self.navigationController pushViewController:self.approve animated:YES];


    }
}






my Origin.m file

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }

    a=(airport*)[app.lstAirports objectAtIndex:indexPath.row];
    NSLog(@"str:%@",a);
    cell.textLabel.text =a.Name;
cell.detailTextLabel.text=a.Code;
    // Configure the cell...

    return cell;
}




#pragma mark -
#pragma mark Table view delegate

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



    a.Name = [app.lstAirports objectAtIndex:indexPath.row];

    // Send notification of new selection
    NSDictionary *userDict = [NSDictionary dictionaryWithObject:a.Name  forKey:@"Name"];
    NSLog(@"str:%@",userDict);
    [[NSNotificationCenter defaultCenter] postNotificationName:NameControllerDidSelectNotification object:self userInfo:userDict];
                 }

3 个答案:

答案 0 :(得分:1)

似乎是一个协议和委托的案例..更好地阅读一些教程....例如.. http://iosdevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.htmlv

答案 1 :(得分:1)

实现此目的的一种方法是通过通知。注册您的更高级表视图控制器以接收NameControllerDidSelectNotification(或类似)。在第二级表视图控制器中,当调用tableView:didSelectRowAtIndexPath:时,将新选择添加到字典对象并将其发布在通知中:

#define NameControllerDidSelectNotification    @"NameControllerDidSelectNotification"


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

    // Store the selection
    self.name = [self.namesArray objectAtIndex:indexPath.row];

    // Send notification of new selection
    NSDictionary *userDict = [NSDictionary dictionaryWithObject:self.name forKey:@"Name"];
    [[NSNotificationCenter defaultCenter] postNotificationName:NameControllerDidSelectNotification object:self userInfo:userDict];
}

然后回到顶级视图控制器中,首先注册为通知的观察者(执行此操作的一个好处是当您按下第二级控制器时),并告诉它触发nameControllerDidSelect:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nameControllerDidSelect:) name:NameControllerDidSelectNotification object:nextController];

    // ...
    // [self.navigationController pushViewController:nextController animated:YES];
    // ...
}

- (void)nameControllerDidSelect:(NSNotification *)notification
{
    NSDictionary *userDict = [notification userInfo];
    self.selectedName = [userDict objectForKey:@"Name"];

    // Only do this here if your controller automatically pushes back to top-level on selecting a name, else put it in -viewWillAppear
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

答案 2 :(得分:0)

我写了一个small example project,演示了如何使用委托从第二个视图控制器传回信息。

这很粗糙 - 但你应该明白这一点。