这是我认为基本的问题,因为stackoverflow充满了它和ofc谷歌。但没有任何帮助我。我需要将表示选择了哪一行的整数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中使用它?
答案 0 :(得分: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;
}
答案 1 :(得分:1)
您正在做的是为班级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.
希望它有所帮助! :)