Mangy92
Mangy92

Reputation: 631

How to pass a variable to the next view?

My app has an variable called int antalratt, which is the number of correct answers in that view. Now I want to pass that variable to the next view, where I want to get the number of correct answers to be shown! I know how to get an integer to a label text though!

The int antalratt is written in the firstviewcontroller.m, how do I make it "global" so that I can use it in the secondviewcontroller?

Thanks in advance!

Upvotes: 0

Views: 128

Answers (4)

Ravi Kumar Karunanithi
Ravi Kumar Karunanithi

Reputation: 2218

filename *detailViewController = [[filename alloc] initWithNibName:@"filename" bundle:nil];
detailViewController.audio=@"yourData";
[self presentModalViewController:detailViewController animated:YES];
[detailViewController release];

Declare in filename.h

NSString *audio;

@property(nonatomic,retain) NSString *audio;

and filename.m

@synthesize audio;


 -(void) ViewDidLoad
 { 
      NSLog(@"Audio = %@",audio);   // if ur variable is integer declare %d in nslog.
 }

thats all

Upvotes: 1

Shaheen M Basheer
Shaheen M Basheer

Reputation: 1010

Method 1:

RootViewController

-(IBAction)nextPage{

    int antalratt = 12;    // Value to be transfered

    FirstViewController * fvc = [[FirstViewController  alloc] initWithNibName:@"FirstViewController" bundle:nil];
    fvc.answer = antalratt;
    [self presentModalViewController:fvc animated:YES];
    [fvc release];

}

FirstViewController

@interface FirstViewController : UIViewController
{

    int answer;
}
@property(nonatomic,assign) int  answer;



@implementation FirstViewController
@synthesize answer;

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"%d",answer);   // //displays answer on log
}

@end

Method 2 (AppDelegate)

AppDelegate

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{

    int antalratt;
}
@property(nonatomic ,assign) int antalratt;

RootViewController

-(IBAction)nextPage{

    int antalratt = 12;    // Value to be transfered
    AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
    delegate.antalratt = antalratt;

    FirstViewController * fvc = [[FirstViewController  alloc] initWithNibName:@"FirstViewController" bundle:nil];
    [self presentModalViewController:fvc animated:YES];
    [fvc release];
}

FirstViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
    NSLog(@"%d",delegate.antalratt);  //displays answer on log
}

Method 3 (NSUserDefaults)

RootViewController

-(IBAction)nextPage{
    int antalratt = 12;    // Value to be transfered
    [[NSUserDefaults standardUserDefaults] setInteger:antalratt forKey:@"answer"];
    FirstViewController * fvc = [[FirstViewController  alloc] initWithNibName:@"FirstViewController" bundle:nil];
    [self presentModalViewController:fvc animated:YES];
    [fvc release]; }

FirstViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    int ans = [[[NSUserDefaults standardUserDefaults] objectForKey:@"answer"] intValue];
    NSLog(@"%d",ans);  //displays answer on log
}

Upvotes: 1

superGokuN
superGokuN

Reputation: 1424

// view1.h

@interface view1 : UIView{

NSString *passingVariable;

}

@property (nonatomic, strong) NSString *passingVariable;

@end

//

view1.m

@synthsize passingVariable;

@implementation view1

@end

// in another view

view2.m

#import "view1.h"

@implementation view2

-(IBAction)changeview

{

  view1 *myview = [[view1 alloc]init];

  myview.passingVariable = [NSString stringWithString:@"Hello Variable"];

  [self.navigationController pushViewController:myview animated:YES];

 }

@end

Upvotes: 0

Quirin
Quirin

Reputation: 720

make a variable in the public interface of secondviewcontroller.h

@property (nonatomic, strong) NSNumber *correctAnswers;

synthesize it in .m and then pass the value of antalratt in firstviewcontroller with secondviewcontroller.correctAnswers = [NSNumber numberWithInt:antalratt];

to secondviewcontroller. then set the labeltext

Upvotes: 1

Related Questions