Reputation: 1866
I'm making a client-server program for iphone and i want to use my serverIP which is a part of my second view in the first view serverIP is a uitextfield. i use to enter the value of ServerIP in second View but i want to use the value of serverIP IN Firstview.
""secondview.h"" interface file
#import <UIKit/UIKit.h>
@interface secondview : UIViewController {
IBOutlet UIView *view;
IBOutlet UITextField *serverIP;
IBOutlet UITextField *noc;
IBOutlet UIButton *save;
IBOutlet UIButton *back;
IBOutlet UIButton *load;
IBOutlet UILabel *display1;
}
-(IBAction) back;
-(IBAction) save;
-(IBAction) load;
@property (nonatomic,retain) IBOutlet UITextField *serverIP;
@property (nonatomic,retain) IBOutlet UITextField *noc;
@property (nonatomic , retain) IBOutlet UILabel *display1;
@end
""secondview.m"" implementation file
#import "secondview.h"
@implementation secondview
@synthesize serverIP,noc,display1;
-(IBAction) save{
[[NSUserDefaults standardUserDefaults] setInteger:serverIP forKey:@"save"];
NSUserDefaults *myname = [NSUserDefaults standardUserDefaults];
[serverIP resignFirstResponder];
}
-(IBAction) load {
serverIP = [[NSUserDefaults standardUserDefaults] integerForKey:@"load"];
NSUserDefaults *myname = [NSUserDefaults standardUserDefaults];
}
-(IBAction) back {
[self.parentViewController dismissModalViewControllerAnimated: YES];
}
- (void)dealloc {
[super dealloc];
}
@end
Upvotes: 2
Views: 160
Reputation: 826
Create a string in apdelegate and set property in it and then assign your textfield value to the appdelegate string.
like in firstview
SpeakersAppDelegate *mainDelegate = (SpeakersAppDelegate *)[[UIApplication sharedApplication] delegate];
mainDelegate.addDelegateGlobalstring=firstviewtextFieldString;
Upvotes: 0
Reputation: 44633
Since you have already done this
[[NSUserDefaults standardUserDefaults] setInteger:serverIP forKey:@"save"];
I will help you on this. First of all, serverIP
is an instance of UITextField
. If you do as above, you will not be storing the text value held in it. Do this instead –
[[NSUserDefaults standardUserDefaults] setString:serverIP.text forKey:@"serverIP"];
[[NSUserDefaults standardUserDefaults] synchronize];
Now you will be able to access this in the first view. You will have to do this –
NSString *serverIP = [[NSUserDefaults standardUserDefaults] stringForKey:@"serverIP"];
An Alternate Approach
However using user defaults might not be the right way. As mentioned above, you can use the application delegate to store all such values which might bloat it up.
It be better to define a model object (mutable) that can be passed to second view controller from the first. Then you can modify the model object in the second view controller and first view controller should be able to see it.
I suggest you declare a retained property on the second view controller as NSMutableString *ipAddress
which you can modify as [ipAddress setString:serverIP.text];
. You will have to declare an iVar in the first view controller and initialize it before passing it along to the second view controller. Let me know if you need additional help on this.
Upvotes: 1
Reputation: 171
The way I'd approach this is to have an NSString instance variable for the IP Addreesin my App Delegate, say
NSString *ipAddress;
You can then set pointers to your App Delegate from both of your Views:
MyAppsAppDelegate *appDelegate;
set the UITextField from appDelegate.ipAddress and change from second view. You might just need to refresh the UITextField in the first View by overriding the viewWillAppear method
Hope that helps
Jeremy
Upvotes: 0