1

皆さん、ここ 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

}
}

君たちありがとう!

4

3 に答える 3

1

こんにちは私はあなたがmyIntegerVeriableを合成するのに欠けていると思いますあなたのsecondviewcontroller.mファイルを変更します

SecondViewControl.m

@synthesize myInteger;

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

うまくいけば、これはあなたを助けるでしょう:)

于 2012-10-17T18:49:32.673 に答える
1

あなたのmyIntegeriVarは、コンパイラがiVarを生成し、プロパティのゲッター/セッターを自動的に合成する方法のb / cで使用されていません。

コンパイラは、プロパティを宣言するときに役立つので、デフォルト以外の動作が必要でない限り、独自の iVar を宣言したり @synthesize を使用したりする必要はありません。

この行@property(nonatomic) NSInteger myInteger;により、コンパイラは実装で次のものと同等のものを生成します。

@synthesize myInteger = _myInteger;

したがって、デフォルト セッターによって変更される iVar は です_myInteger

SecondViewController で次のいずれかを実行できます。私はソリューション#1 b / cを好みます。これは、よりクリーンでコードが少なく、自動コンパイラの動作を利用しています。

  1. SecondViewController.h で iVar を削除し、myIntegerSecondViewController.m で iVar への参照を または のいずれかに変更します_myIntegerself.myInteger

また

  1. SecondViewController.m で、プロパティを明示的に合成して、iVar を使用するように追加します。@synthesize myInteger;

編集:特定の例を追加

// 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];
    }
}
于 2012-10-17T19:05:25.843 に答える
0

prepareForSegue:sender:Xcode 4.5 で Storyboards を使用していると仮定すると、パラメーターを次のパラメーターに渡すことはメソッドで行う必要があります。

- (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: メソッドでセグエを実行できます。

[self performSegueWithIdentifier:@"YOUR_SEGUE_NAME_HERE"];

そして、SecondViewController のヘッダー ファイルに次のようなプロパティがあると仮定すると、パラメーターを 2 番目のビューに再度渡す必要があります。

@property (nonatomic, assign) int *selectedIndex;

編集:

あなたがやろうとしていることとの関連で、FirstViewController の一番上にあるプライベート プロパティを簡単に作成し、selectedIndex を didSelectRowAtIndexPath: から保存して、それを prepareForSegue: に渡すことができます。

@interface FirstViewController() {
    int selectedIndex;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath  *)indexPath {
    selectedIndex = indexPath.row
}
于 2012-10-17T18:15:23.717 に答える