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
Since you have already done this
I will help you on this. First of all,
serverIPis an instance ofUITextField. If you do as above, you will not be storing the text value held in it. Do this instead –Now you will be able to access this in the first view. You will have to do this –
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 *ipAddresswhich 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.