1

这是我认为的基本问题,因为 stackoverflow 充满了它,ofc google 也是。但没有任何帮助。我需要将表示选择哪一行的整数值传递给另一个类。这是我的代码:

表视图控制器.h

#import <UIKit/UIKit.h>

@interface TableViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

{
    NSArray *tabledata;
}
@property (nonatomic, retain) NSArray *tabledata;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic) NSInteger selectedRow;

@end

表视图控制器.m

#import "Instruments.h"

@interface Instruments ()

@end

@implementation Instruments
@synthesize tabledata;
@synthesize tableView;
@synthesize selectedRow;

- (void)viewDidLoad
{
    [super viewDidLoad];
    tabledata = [[NSArray alloc] initWithObjects:@"Row1", @"Row2", @"Row3", nil];
    self.selectedRow = 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tabledata count];
}

- (UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    cell = [tableview dequeueReusableCellWithIdentifier:@"Row1"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Row1"];
    }

    cell.textLabel.text = [tabledata objectAtIndex:indexPath.row];

    if (indexPath.row == self.selectedRow)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

- (void)tableView:(UITableView *)tableview didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row != self.selectedRow) {
        self.selectedRow = indexPath.row;
    }

    [tableView reloadData];
}


- (void)viewDidUnload {
    [self setTableView:nil];
    [super viewDidUnload];
}
@end

第一视图控制器.h

//Some code which calls my variable 'selectedrow'
...

第一视图控制器.m

...
if selectedrow = 0 {
//some code
}
if selectedrow = 1 {
//some code
}
if selectedrow = 2 {
//some code
}
...

我应该如何声明我的变量“selectedrow”才能在 FirstViewController 中使用它?

4

2 回答 2

1

在 MVC 世界中,您打算在多个视图控制器之间共享的所有内容都属于模型。您的模型类在全球范围内可用;一个控制器设置值;另一个读它。

头文件:MyAppModel.h

@interface MyAppModel // <<<=== Use an appropriate name here
+(MyAppModel*)instance;
@property (readwrite) NSInteger selectedRow;
@end

实现:MyAppModel.m

@implementation MyAppModel
+(MyAppModel*)instance {
    static MyAppModel *statInst;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        statInst = [[MyAppModel alloc] init];
    });
    return statInst;
}
@synthesize selectedRow;
@end

用法:你的视图控制器

// Set the selected row
[MyAppModel instance].selectedRow = indexPath.row;

// Access the last selected row
switch ([MyAppModel instance].selectedRow) {
    case 1: // some code
    break;
    case 2: // some code
    break;
    case 3: // some code
    break;
}
于 2012-10-27T11:46:08.767 回答
1

您正在做的是为 Class 设置一个变量,TableViewController而不是为FirstViewController

因此,在TableViewController您呈现 FirstViewController 的视图时,也要设置该值。例如,

 FirstViewController* newObject = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
 newObject.selectedIndex = self.selectedIndex;   //Here self indicates the present class which is `TableViewController`
 //Now whatever your insertion method.  i.e
 [self.navigationController pushViewController:newObject animated:Yes];
 // or presenting your modal view controller, this depends totally on you.

希望能帮助到你!:)

于 2012-10-27T11:49:58.303 回答