在另一类中使用选定的行



这是我认为的基本问题,因为stackoverflow充满了它和OFC Google。但是没有任何帮助。我需要将整数Vaule传递给另一个类,然后将选择哪一行选择。这是我的代码:

tableviewcontroller.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

tableviewcontroller.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

firstViewController.h

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

firstViewController.m

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

我应该如何声明我的变量" Selectedrow"以在FirstViewController中使用它?

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

标题: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;
}

您正在做的是为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.

希望它有帮助!:)

相关内容

  • 没有找到相关文章

最新更新