Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 1st, 2012  |  syntax: None  |  size: 2.92 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to pass a variable to the next view?
  2. @property (nonatomic, strong) NSNumber *correctAnswers;
  3.        
  4. -(IBAction)nextPage{
  5.  
  6.     int antalratt = 12;    // Value to be transfered
  7.  
  8.     FirstViewController * fvc = [[FirstViewController  alloc] initWithNibName:@"FirstViewController" bundle:nil];
  9.     fvc.answer = antalratt;
  10.     [self presentModalViewController:fvc animated:YES];
  11.     [fvc release];
  12.  
  13. }
  14.        
  15. @interface FirstViewController : UIViewController
  16. {
  17.  
  18.     int answer;
  19. }
  20. @property(nonatomic,assign) int  answer;
  21.  
  22.  
  23.  
  24. @implementation FirstViewController
  25. @synthesize answer;
  26.  
  27. - (void)viewDidLoad
  28. {
  29.     [super viewDidLoad];
  30.  
  31.     NSLog(@"%d",answer);   // //displays answer on log
  32. }
  33.  
  34. @end
  35.        
  36. @interface AppDelegate : UIResponder <UIApplicationDelegate>
  37. {
  38.  
  39.     int antalratt;
  40. }
  41. @property(nonatomic ,assign) int antalratt;
  42.        
  43. -(IBAction)nextPage{
  44.  
  45.     int antalratt = 12;    // Value to be transfered
  46.     AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
  47.     delegate.antalratt = antalratt;
  48.  
  49.     FirstViewController * fvc = [[FirstViewController  alloc] initWithNibName:@"FirstViewController" bundle:nil];
  50.     [self presentModalViewController:fvc animated:YES];
  51.     [fvc release];
  52. }
  53.        
  54. - (void)viewDidLoad
  55. {
  56.     [super viewDidLoad];
  57.     // Do any additional setup after loading the view from its nib.
  58.     AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
  59.     NSLog(@"%d",delegate.antalratt);  //displays answer on log
  60. }
  61.        
  62. -(IBAction)nextPage{
  63.     int antalratt = 12;    // Value to be transfered
  64.     [[NSUserDefaults standardUserDefaults] setInteger:antalratt forKey:@"answer"];
  65.     FirstViewController * fvc = [[FirstViewController  alloc] initWithNibName:@"FirstViewController" bundle:nil];
  66.     [self presentModalViewController:fvc animated:YES];
  67.     [fvc release]; }
  68.        
  69. - (void)viewDidLoad
  70. {
  71.     [super viewDidLoad];
  72.     // Do any additional setup after loading the view from its nib.
  73.     int ans = [[[NSUserDefaults standardUserDefaults] objectForKey:@"answer"] intValue];
  74.     NSLog(@"%d",ans);  //displays answer on log
  75. }
  76.        
  77. filename *detailViewController = [[filename alloc] initWithNibName:@"filename" bundle:nil];
  78. detailViewController.audio=@"yourData";
  79. [self presentModalViewController:detailViewController animated:YES];
  80. [detailViewController release];
  81.        
  82. NSString *audio;
  83.  
  84. @property(nonatomic,retain) NSString *audio;
  85.        
  86. @synthesize audio;
  87.  
  88.  
  89.  -(void) ViewDidLoad
  90.  {
  91.       NSLog(@"Audio = %@",audio);   // if ur variable is integer declare %d in nslog.
  92.  }
  93.        
  94. @interface view1 : UIView{
  95.  
  96. NSString *passingVariable;
  97.  
  98. }
  99.  
  100. @property (nonatomic, strong) NSString *passingVariable;
  101.  
  102. @end
  103.        
  104. view1.m
  105.  
  106. @synthsize passingVariable;
  107.  
  108. @implementation view1
  109.  
  110. @end
  111.        
  112. view2.m
  113.  
  114. #import "view1.h"
  115.  
  116. @implementation view2
  117.  
  118. -(IBAction)changeview
  119.  
  120. {
  121.  
  122.   view1 *myview = [[view1 alloc]init];
  123.  
  124.   myview.passingVariable = [NSString stringWithString:@"Hello Variable"];
  125.  
  126.   [self.navigationController pushViewController:myview animated:YES];
  127.  
  128.  }
  129.  
  130. @end