Reputation: 557
I am making an application solely for the purpose of learning more objective c. This application basically takes a user to a modal view and then gives the user options (uibuttons) to click. When a user taps a button, the modal view is dismissed and I would like to take the data taken from that button (whether it's the title of the button, tag, etc, whichever is easiest to use) and store it as a variable in my main view. I have tried using an extern NSString defined in a seperate .h file, but no luck. What am I missing?
Upvotes: 0
Views: 279
Reputation: 27050
suppose you've two viewcontrollers say A & B
Your A.h
{
NSString *strData;
int cId;
}
@property (nonatomic, retain) NSString *strData;
@property (readwrite) int cId;
Now In your A.m
@synthesize strData,cId;
Your B.h
@class A
{
A *aObj;
}
Now In your B.m
#import "A.h"
- (void) viewDidLoad
{
aObj=[A alloc] init]; //alloc object of A
[aObj setCId:10]; //set value for cId
[aObj setStrData:@"Hello from B"]; //set value for strData
//do what ever
[aObj release]; //don't forget
}
Upvotes: 0
Reputation: 2122
You can store the data in an object in the appDelegate which is the Delegate file for the applications,
You can declare an object in appDelegate:
NSString *buttonName;
Then propertise and synthesise that object.
After doing this, put a code in the View Controller which you are opening as a Modal VIew:
appDelegate.buttonName = yourbutton.titleLabel.text;
Now even after dismissing the modal view, you'll be having the button's title stored in the appldelegate's object, and you can access it from any where in your application.
Upvotes: 0
Reputation: 37729
use delegations. make a protocol.
@protocol SelectValueDelegate <NSObject>
@optional
- (void) selectedValue:(NSString * )values selectionViewController:(UIViewController *)controller;
- (void)selectionCanceled:(UIViewController *)controller;
@end
implement it inside your MainViewController.h
@interface MainViewController : UIViewController<SelectValueDelegate> {
//....
}
and .m
file looks like this:
- (void) selectedValue:(NSString * )values selectionViewController:(UIViewController *)controller
{
//here you have value.
[controller dissmissModalViewControllerAnimated:YES];
}
- (void)selectionCanceled:(UIViewController *)controller
{
[controller dissmissModalViewControllerAnimated:YES];
}
and in you ModalViewController
make a property of delegate like this:
@interface ModalViewController : UIViewController
id<SelectValueDelegate> delegate;
}
@property(assign)id delegate; // synthesize it also
now on click of button do something like this:
-(IBAction)buttonClicked:(id)sender
{
[delegate selectedValue:@"Value" selectionViewController:self];
}
and when presenting it modally inside MainViewController
, do something like this:
ModalViewController *screen = [[ModalViewController alloc] initWithBlahblah];
screen.delegate = self;
[self.navigationController presentModalViewControllerAnimated:YES];
Upvotes: 1
Reputation: 83
you have to use the delegate methods to pass value see Apple documentation
Upvotes: 0
Reputation: 17478
You have to go through delegate pattern in iOS.
You can pass the value back to the main view using delegate methods.
Apple documentation on Protocols.
Upvotes: 0