1

I currently have a class and ViewController with a button action to get the username and password from a textfield and put them into their own NSString. I then use the NSString to perform a post request like so.

NSString *user = _username.text;
    NSString *password = _password.text;


    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://thesite.com/login.php"]];
    [httpClient setParameterEncoding:AFFormURLParameterEncoding];
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
                                                            path:@"http://thesite.com/login.php"
                                                      parameters:@{@"username":user, @"password":password}];
    AFHTTPRequestOperation * httpOperation = [httpClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //success code
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //error handler
    }];
    [httpClient enqueueHTTPRequestOperation:httpOperation];

However I have another class and Viewcontroller to perform a get request. However, in order to perform this get request I need to get the "NSString *user" from the first View Controller. How would I go about doing this? Should I declare a NSString *user in the header of the first Viewcontroller and then in the second View controller declare an instance of the first class?

3
  • How do you perform switching from one ViewController to another? Commented Jan 24, 2014 at 6:55
  • send as argument better approach i think Commented Jan 24, 2014 at 6:56
  • you can use NSNotificationCenter Commented Jan 24, 2014 at 7:00

5 Answers 5

1

You can pass strings through viewcontrollers. Make therefore a segue between the two viewcontroller and named it for example "secondVC"

the when you want to switch to other view make this call

[self performSegueWithIdentifier:@"secondVC"];

and implement this method.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"secondVC"]) {
    SecondViewController *second = (SecondViewController *)[segue destinationViewController];
    second.userString = self.user;
}
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use NSUserDefaults to store the username and use it in other ViewControllers.

For example, save it in your current ViewController.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:username forKey:@"UserName"];
[defaults synchronize];

get username it in another ViewController.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *username = [defaults objectForKey:@"UserName"];

Comments

0

pass the userName to second view controller.

You can achieve it using following approaches.

try to pass the username when you are initialising your secondViewController like

SecondViewController * sVC = [SecondViewController alloc] initWithUserName: username];

for this in your SecondViewController class you have to modify the init method and add a property of userName {typeOF string}

@property (nonatomic, strong) NSString * userName

- (id) initWithUserName: (NSString *) name
{
    self = [super initWithNibName: @"SecondViewController"
                           bundle: nil];
    if (self)
    {
        self.userName = name;
    }
    return self;
}

Enjoy!!

Comments

0

Create object of first class in second class like this..

//First class

//Pass the values to seconClass

secondClass *appdelegate = [NSApp delegate];
[appdelegate initwithDetails:userName withPassword:password];

//Second class

//In second class declare the function and get the values

//When u create the object, init function will call first

-(id)initwithDetails:(NSString *)user withPassword:(NSString *)password 
{
    userName = [NSString stringWithFormat:@"%@", user];
    newPass=[NSString stringWithFormat:@"%@", password];
    return self;
}

Comments

0

If the second viewcontroller is spawned from the first you could just create a delegate protocol and corresponding delegate so that either the first is delegate of the second or the second is delegate of the first:

Here is an example of a protocol which includes one method, notice the instance variable delegate is of type id, as it will be unknown at compile time the type of class that will adopt this protocol.

#import <Foundation/Foundation.h>

@protocol ProcessDataDelegate <NSObject>
@required
- (void) processSuccessful: (BOOL)success;
@end

@interface ClassWithProtocol : NSObject 
{
 id <ProcessDataDelegate> delegate;
}

@property (retain) id delegate;

-(void)startSomeProcess;

@end

Inside the implementation section for the interface defined above we need to do two things at a minimum – first synthesize the delegate instance variable and second, call the method defined in the protocol as needed (more on that in a moment).

Let’s look at a bare bones implementation of the ClassWithProtocol.m:

#import "ClassWithProtocol.h"

@implementation ClassWithProtocol

@synthesize delegate;

- (void)processComplete
{
  [[self delegate] processSuccessful:YES];
}

-(void)startSomeProcess
{
  [NSTimer scheduledTimerWithTimeInterval:5.0 target:self 
    selector:@selector(processComplete) userInfo:nil repeats:YES];
}

@end

Read more at this tutorial

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.