在另一个班级中使用选定的行 [英] Use selected row in another class

查看:97
本文介绍了在另一个班级中使用选定的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我认为基本问题,因为stackoverflow充满了它和ofc谷歌。但没有什么帮助我。我需要传递一个整数值来说明哪一行被选中,然后再传给另一个类。这是我的代码:

TableViewController.h

  #import< ; UIKit的/ UIKit.h> 

@interface TableViewController:UIViewController< UITableViewDataSource,UITableViewDelegate>

{
NSArray * tabledata;
}
@property(nonatomic,retain)NSArray * tabledata;
@property(强,非原子)IBOutlet UITableView * tableView;
@property(非原子)NSInteger selectedRow;

@end

TableViewController.m

  #importInstruments.h

@interface乐器()

@end

@implementation乐器
@synthesize tabledata;
@synthesize tableView;
@synthesize selectedRow;

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

$ b - (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;

$ b - (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

  //调用变量'selectedrow'的一些代码
...
$ b $ FirstViewController.m

$ $ p $ ...
if selectedrow = 0 {
//某些代码
}
如果selectedrow = 1 {
//某些代码
}
如果selectedrow = 2 {
//一些代码
}
...

我该如何声明我的变量'selectedrow'能够在FirstViewController中使用它吗?

解决方案

在MVC世界中,在多个视图控制器中属于模型。你的模型类是全球可用的;一个控制器设置该值;

头文件:MyAppModel.h

  @接口MyAppModel //<<<<< ===在这里使用适当的名称
+(MyAppModel *)实例;
@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];
});
返回statInst;
}
@synthesize selectedRow;
@end

用法:您的视图控制器

  //设置选中的行
[MyAppModel instance] .selectedRow = indexPath.row;

//访问最后选择的行
switch([MyAppModel instance] .selectedRow){
case 1:// some code
break;
案例2://一些代码
break;
案例3://一些代码
break;
}


this is I think basic problem because stackoverflow is full of it and ofc google too. But nothing helped me. I need to pass integer vaule which says which row is selected, to another class. Here is my code:

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

How should I declare my variable 'selectedrow' to be able to use it in FirstViewController?

解决方案

In the MVC world, everything that you intend to share among multiple view controllers belongs in the model. Your model class is globally available; one controller sets the value; the other reads it.

Header: MyAppModel.h

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

Implementation: 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

Usage: your view controllers

// 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;
}

这篇关于在另一个班级中使用选定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆