Reputation: 803
I'm new to Objective-C and have a question. Did the search multiple times but I couldn't find what I was looking for.
I'm using storyboard for this app. On the homescreen you've got some buttons with labels above them. Those labels should tell a number. When pushing the button you go to a new viewController where you have input that (after 'save') goes back to the homescreen and updates the label with the correct number. All that works great for one button and I'm very happy about it.
The problems are:
1. Since I have multiple buttons with labels, I want to use the same viewController to give input over and over again. I tried connecting every button to slide to the viewController under the identifier "AddData", but Xcode doesn't allow the same identifiers twice or more in storyboard. So I would need something else for this. Any idea?
2. Currently I use the following code to bring back the data to the homescreen:
homeScreenViewController
- (IBAction)unwindToHomeScreen:(UIStoryboardSegue *)segue;
{
inputDataViewController *source = [segue sourceViewController];
self.logoOneLabel.text = source.endTotalNumber;
}
inputDataViewController:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if (sender != self.saveButton) {
return;
} else {
if (endTotalLabelNumber > 0) {
self.endTotalNumber = [NSString stringWithFormat:@"%.0f", totalLabelNumber + endTotalLabelNumber];
} else if (endTotalLabelNumber == 0 && totalLabelNumber == 0){
self.endTotalNumber = 0;
} else {
self.endTotalNumber = [NSString stringWithFormat:@"%.0f", totalLabelNumber + endTotalLabelNumber];
}
}
}
This works great for the one button, but how to use this with multiple? I heard about Delegates to use the same viewController multiple time and get data back to different places, but I just don't get it. Any help?
Upvotes: 2
Views: 2031
Reputation: 152
There is a few different way to implement what you need. But i think most common its a delegate. This is how your inputDataViewController looks like:
#import <UIKit/UIKit.h>
@protocol inputDataDelegate;
@interface inputDataViewController : UIViewController
@property (weak) id<inputDataDelegate> delegate;
@property (strong, nonatomic) NSNumber *buttonTag;
@end
@protocol inputDataDelegate <NSObject>
-(void) inputDataViewControllerDismissed:(id)data;
@end
Then in @implementation, you should in "save" button action, message to you delegate method :
[self inputDataViewControllerDismissed:@{@"buttonTag":buttonTag,@"endTotalNumber":endTotalNumber}
Next in homeScreenViewController connect delegate :
@interface homeScreenViewController : UIViewController<inputDataDelegate>
After that in @implementation:
-(void)inputDataViewControllerDismissed:(id)data
{
// if you use modal
[self dismissViewControllerAnimated:YES completion:nil];
// or if you use push
//[self.navigationController popViewControllerAnimated:YES];
switch (data[@"buttonTag"]) {
case 1:
self.lableWtiTagOne = data[@"endTotalNumber"];
break;
case 2:
self.lableWtiTagTwo = data[@"endTotalNumber"];
break;
// number of cases depend how many buttons you have
}
Also, most important, thing didn't forget send self to our delegate:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"inputDataController"])
{
inputDataViewController *inputCtrl = [segue destinationViewController];
inputCtrl.delegate = self;
inputCtrl.buttonTag = sender.tag
}
}
Upvotes: 1
Reputation: 62062
You shouldn't need delegates.
What you will need is a property on the view controller that handles input to it knows which button it is handling input for.
When you segue to the input controller, set this property, based on which button was pushed. When you unwind back, fetch this property to know which label to modify.
For example, in your input view controller's .h
file, add a property like this:
@property (nonatomic,assign) NSInteger handlingTag;
Or something, whatever name makes sense to you.
Now you need to implement your home screen view controller's prepareForSegue:sender:
.
Use the sender
argument to determine which button was pushed, and based on that, set the input view controller's new handlingTag
property based on the button in a way that you will know what to do with it when we unwind.
Now in the unwind method:
switch (source.handlingTag)
Create a switch structure based on the source's handlingTag
property, and set the appropriate label based on this value.
As Jeff points out in the comments, it'd be a really good idea to define an NS_ENUM
to use here for the property rather than an NSInteger
. The NS_ENUM
would allow you to name the values you're using.
Upvotes: 3