views:

34

answers:

2

I'm having a tableview with a couple of cells with 4 uiswitches and one textfield inside. Simple question. I need a way to read all the positions of the switches and the string inside the textview by clicking a button. Tableview, textfield, switches and button is working fine. Just need to access the values of the switches and textfield from another method. By tagging the switches or something.

So how can I access the values from another method. Something like in tableview method:

...

    UISwitch *switchView = [[option1 alloc] initWithFrame:CGRectZero];
    switchView.tag = 3000;

    cell.accessoryView = switchView;

...

And something like this in the button method:

...

BOOL status = [self.view viewWithTag:3000].on; //*Not a working method*
A: 

You are in the same class, nop ?

So maybe you could put your switch & co as class variable (declared in the header file). It would be easier ^^

Vinzius
Simple, good idea. But how would put the declared uiswitch in the tableview. In header: UISwitch *switch1; In tableview-method: cell.accessoryView = switch1; In method to look at the switchs status: if (switch1.on == YES ) ??? Is not working. Switch doesnt get included in tableview correct.
John Kofod
Could you put some codes, because it should work.
Vinzius
A: 

Sorry. I declared the switch in my flipsideview header:

    @protocol FlipsideViewControllerDelegate;


@interface FlipsideViewController : UIViewController {
    id <FlipsideViewControllerDelegate> delegate;

    NSMutableArray *list;   
    UIView *myView;

    UISwitch *switch1;
}

@property (nonatomic, copy, readwrite) NSMutableArray *list;
@property (nonatomic, retain) UIView * myView;

@property (nonatomic, retain)UISwitch *switch1;

@property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate;
- (IBAction)cancel:(id)sender;
- (IBAction)save:(id)sender;

@end


@protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
@end

Then I have the tableview together with the bar, in the flipsideview. Removed the code that isn't relevant to the question:

- (void)viewDidLoad {

    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, 320, 416) style:UITableViewStyleGrouped];
    tableView.dataSource = self;
    tableView.allowsSelection = FALSE;

    [self.view addSubview:tableView];

    [super viewDidLoad];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,0,0) reuseIdentifier:@"CellIdentifier"] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    if(indexPath.row == 0) {            
        cell.text  = @"ON/OFF";         
        cell.accessoryView = [[switch1 alloc] initWithFrame:CGRectZero]; //Doesnt work, and can't reference!!!      
    }

    else {          
        cell.accessoryView = [[UITextField alloc] initWithFrame:CGRectMake(0,0,284,30)];
    }

    return cell;
}

If I replace the commented line with:

cell.accessoryView = [[UISwitch alloc] initWithFrame:CGRectZero];

It of course works, but then I can't reference the value of it later in another method like this:

    - (IBAction)save:(id)sender {
    if (option1.on == YES ) {
        [self.delegate flipsideViewControllerDidFinish:self];
    }
}
John Kofod