Question

Right now I have a kind of pop-up selector so that the user can pick an option on my screen. Please see this sample project to see what I mean.

I'm using the same method as this sample, except that I have three different buttons where the user will select. My problem is that I am using this code:

- (void)itemSelectedatRow:(NSInteger)row
{
    NSLog(@"row %lu selected", (unsigned long)row);
    [self.selectSeverity setTitle:[self.severityArray objectAtIndex:row] forState:UIControlStateNormal];
    [self.selectStatus setTitle:[self.statusArray objectAtIndex:row] forState:UIControlStateNormal];
    [self.selectGender setTitle:[self.genderArray objectAtIndex:row] forState:UIControlStateNormal];
}

...and it is changing the name of the button for all three every time the user selects one. So for example, if the user taps the "selectSeverity" button, and chooses the item on row three, the name for the selectStatus and selectGender button will also change to the item on row three on its own corresponding array.

What I need to do is somehow separate this method so the button's title changes only when a row has been selected in its own array: how can I do this?

More information: I have these tableViews embedded in a Navigation Controller: statusViewController.m/.h genderViewController.m/.h pickerViewController.m/.h (this corresponds to the Severity button)

Each have a delegate with a separate ID, but the same content:

@protocol correspondingViewControllerDelegateHere <NSObject>
@required
    - (void)itemSelectedatRow:(NSInteger)row;
@end

@interface correspondingViewControllerHere : UITableViewController
@property (strong, nonatomic) NSArray *correspondingArrayHere;
@property (assign, nonatomic) id<statusViewControllerDelegate> delegateIdHere;
@end

Each have the same content in the .m file as well, but correspond to their own delegates, arrays, etc.

@implementation statusViewController

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.statusData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"CellIdentifierHere";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
    cell.textLabel.text = [self.statusData objectAtIndex:indexPath.row];

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];

if ([self.delegateIdHere respondsToSelector:@selector(itemSelectedatRow:)]) {
    [self.delegateIdHere itemSelectedatRow:indexPath.row];
    [self dismissViewControllerAnimated:YES completion:nil];
    }
}

@end

In manualViewController.h (this is the view where they are implemented) I have declared the delegates. Lastly, in the file manualViewController.m, I have the following code to implement them:

- (IBAction)showinfo:(id)sender
{
UINavigationController *navigationController = (UINavigationController *)[self.storyboard instantiateViewControllerWithIdentifier:@"pickerVC"];
pickerViewController *tableViewController = (pickerViewController *)[[navigationController viewControllers] objectAtIndex:0];
tableViewController.severityData = self.severityArray;
tableViewController.navigationItem.title = @"Triage Severity Levels";
tableViewController.delegate1 = self;
[self presentViewController:navigationController animated:YES completion:nil];
}


- (IBAction)showStatus:(id)sender {
UINavigationController *navigationController = (UINavigationController *)[self.storyboard instantiateViewControllerWithIdentifier:@"statusVC"];
statusViewController *tableViewController = (statusViewController *)[[navigationController viewControllers] objectAtIndex:0];
tableViewController.statusData = self.statusArray;
tableViewController.navigationItem.title = @"Triage Severity Levels";
tableViewController.delegate3 = self;
[self presentViewController:navigationController animated:YES completion:nil];
}

- (IBAction)showGender:(id)sender {
UINavigationController *navigationController = (UINavigationController *)[self.storyboard instantiateViewControllerWithIdentifier:@"genderVC"];
genderViewController *tableViewController = (genderViewController * [[navigationController viewControllers] objectAtIndex:0];
tableViewController.genderData = self.genderArray;
tableViewController.navigationItem.title = @"Triage Severity Levels";
tableViewController.delegate2 = self;
[self presentViewController:navigationController animated:YES completion:nil];
}

SO as I said before, I think the code lies in the itemSelectedAtRow. Can someone help me separate this method so that the choice of one item does not affect the choice of another item? Thanks

Was it helpful?

Solution

One solution is to change the protocol to require three different methods

@protocol correspondingViewControllerDelegateHere <NSObject>
@required
    - (void)itemSelectedSeverityAtRow:(NSInteger)row;
    - (void)itemSelectedStatusAtRow:(NSInteger)row;
    - (void)itemSelectedGenderAtRow:(NSInteger)row;
@end

Then implement the three methods in the manualViewController and call the appropriate method from each of the other view controllers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top