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

时间:2012-10-17 18:07:04

标签: 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

}
}

谢谢你们!

3 个答案:

答案 0 :(得分:1)

您好我认为您缺少合成整数变量修改您的第二个viewcontroller.m文件

SecondViewControl.m

@synthesize myInteger;

-(void)viewDidLoad {
    NSLog(@" the number is = %d",myInteger); 
}

希望这会对你有所帮助:)。

答案 1 :(得分:1)

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

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

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

@synthesize myInteger = _myInteger;

因此,默认设置器修改的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];
          }
      }
      

答案 2 :(得分:0)

将参数传递到下一个参数应该在prepareForSegue:sender:方法中完成,假设你在Xcode 4.5中使用了Storyboard。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
       SecondViewController *second = (SecondViewController *)segue.destinationViewController;
       second.selectedIndex = selectedIndex;
    }
}

然后在你的didSelectRowAtIndexPath:方法中你可以执行segue:

[self performSegueWithIdentifier:@"YOUR_SEGUE_NAME_HERE"];

它应该再次将参数传递给你的第二个视图,假设你在SecondViewController的头文件中有一个属性,如:

@property (nonatomic, assign) int *selectedIndex;

修改

在你想要做的事情的上下文中,你可以轻松地在你的FirstViewController顶部创建一个私有属性,并存储你的didSelectRowAtIndexPath中的selectedIndex:在那里并在prepareForSegue中传递它:

@interface FirstViewController() {
    int selectedIndex;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath  *)indexPath {
    selectedIndex = indexPath.row
}