iphone - 如何将 UITableViewController 中选定行的数量传递/发送到另一个类?

标签 iphone objective-c ios xcode xcode4.5

伙计们,我在 XCODE4.5 中遇到了一些麻烦,我希望你能帮助我!

如何使用 didSelectRowAtIndexPath 方法将 UITableViewController 中选定行的整数值传递或发送到另一个 ViewController?

这是我的代码:

SecondViewController.h
{
NSInteger myInteger;
}
@property(nonatomic) NSInteger myInteger;

SecondViewControl.m
-(void)viewDidLoad {
NSLog(@" the number is = %d",myInteger); //this is not working, I always get "the number is = 0 "
}


FirstViewController.h
#import "SecondViewController"
//...

FirstViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath  *)indexPath {
if (indexPath) {
 NSIndexPath *path = indexPath;
    NSInteger theInteger = path.row;
 NSLog(@"selected row = %d", theInteger); //code OK

//THE PROBLEM STARTS HERE!!!!!!!!!!!!!!!!!!   

SecondViewController *second = [[SecondViewController alloc]init];
    [second setMyInteger:theInteger];
// i'm trying to use "second.myInteger = theInteger;" , but it's also not working

}
}

谢谢大家!

最佳答案

你的 myInteger iVar 是编译器如何生成 iVar 并自动为属性合成 getter/setter 的未使用 b/c。

编译器会在您声明属性时帮助您,因此您无需声明自己的 iVar 或使用 @synthesize,除非您想要默认行为以外的行为。

@property(nonatomic) NSInteger myInteger; 行导致编译器在您的实现中生成以下等价物。

@synthesize myInteger = _myInteger;

因此,默认setter修改的iVar是_myInteger

您可以在 SecondViewController 中执行以下操作之一。我更喜欢解决方案 #1 b/c,它更简洁、代码更少并且利用了自动编译器行为。

  1. 在 SecondViewController.h 中删除 myInteger iVar,并在 SecondViewController.m 中将对 iVar 的任何引用更改为 _myIntegerself.myInteger

  1. 在 SecondViewController.m 中,通过添加 @synthesize myInteger;
  2. 显式合成属性以使用您的 iVar

编辑:添加具体示例

// SecondViewController.h
@interface SecondViewContoller : UIViewController
@property(nonatomic) NSInteger myInteger;
@end

// SecondViewControl.m
-(void)viewDidLoad {
    NSLog(@" the number is = %d", self.myInteger);
}

// FirstViewController.h
#import "SecondViewController"
//...

// FirstViewController.m
//
// rest of implementation
//
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath  *)indexPath
{
    if (indexPath) {
        NSIndexPath *path = indexPath;
        NSInteger theInteger = path.row;
        NSLog(@"selected row = %d", theInteger);

        SecondViewController *second = [[SecondViewController alloc] init];
        second.myInteger = theInteger;
        // you need to present second somehow, viewDidLoad won't be called until then
        // example if using a navigationController
        [self.navigationController pushViewController:second animated:YES];
    }
}

关于iphone - 如何将 UITableViewController 中选定行的数量传递/发送到另一个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12940559/

相关文章:

ios - RubyMotion 和 InfoPlist.strings 本地化

iphone - 如何检查主机的网络服务

iphone - splitView/Objective C - 无法从第二个 tableViewController 更新 detailView

iphone - HTTP "Content-Disposition: attachment"和在 iOS 网络浏览器上下载文件

ios - 内部类存在于混合框架的自动生成的 header 中

ios - UIButton 的 .touchUpInside 事件不起作用

iphone - 在 iPhone 上调试应用程序,现在我收到此错误 : unknown packet reply: "timeout" to environmental package

ios - 在 Xcode 中停止和启动应用程序会恢复已删除的 CoreData 项目

objective-c - iPhone 中的 Sqlite3 数据库被锁定 - 如何避免?

iOS - 从 UITableView 崩溃中删除行