【问题标题】:how to sent selected value from second class to first class indexpath.row in iphone如何将选定的值从第二类发送到 iPhone 中的第一类 indexpath.row
【发布时间】:2011-09-16 10:12:09
【问题描述】:
 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:]: unrecognized selector sent to instance 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];
                 }

【问题讨论】:

  • 你能用一些语法写文章吗?

标签: iphone objective-c


【解决方案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];
}

【讨论】:

  • self.selectedName 这是什么我试试看这条线出错了
  • @Rocky:我假设您在控制器对象中创建了一个名为selectedName 的属性来存储新选择的名称。您可以随意调用它,只要确保在您的界面中声明它即可。
  • 我没有,但我的应用程序为什么崩溃了
  • @Rocky:抱歉——我的示例代码中有错误。我现在已经更正了。见-(void)tableView:(UITableView *)tableView didSelectRoawAtIndexPath:(NSIndexPath *)indexPath下的第一行代码。
  • 请看我的代码我现在编辑的两个类我做了什么请检查它
【解决方案2】:

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

【讨论】:

  • 你朋友我知道,但是如何使用它你可以一步一步地解释我阅读了这个文件,但没有得到正确
【解决方案3】:

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

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

【讨论】:

  • Abizern 感谢朋友的帮助,但我很困惑我已经完成了,但我的数据没有通过它告诉我 NSCFStTring bec of app.lstAirports 是数组,我想从这个数组名称和代码中获取两个值如此取值 a=(airport*)[app.lstAirports objectAtIndex:indexPath.row]; NSLog(@"str:%@",a); cell.textLabel.text =a.Name; cell.detailTextLabel.text=a.Code;但是当我传递数组时,选择的方法是否传递了 app.lstAirports 或字符串 a.name 的值,我选择的值在 debeger 上显示但值没有传递,请帮助我
  • @Rocky - 我想帮忙,但我不明白你在问什么。对不起。
  • 谢谢朋友,我的问题是解决了,这是我的错误,我传递了字符串值,所以我得到一个错误感谢你的演示应用程序
猜你喜欢
  • 1970-01-01
  • 2021-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多