如何将变量传递给下一个视图?

时间:2012-04-21 10:40:17

标签: iphone objective-c view global-variables

我的应用有一个名为int antalratt的变量,它是该视图中正确答案的数量。现在我想将该变量传递给下一个视图,在那里我想要显示正确答案的数量!我知道如何获得标签文本的整数!

int antalratt写在firstviewcontroller.m中,如何将其设为“全局”,以便我可以在secondviewcontroller中使用它?

提前致谢!

4 个答案:

答案 0 :(得分:1)

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

在filename.h中声明

NSString *audio;

@property(nonatomic,retain) NSString *audio;

和filename.m

@synthesize audio;


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

那就是

答案 1 :(得分:1)

在secondviewcontroller.h的公共接口中创建一个变量

@property (nonatomic, strong) NSNumber *correctAnswers;

在.m中合成它,然后在firstviewcontroller中使用secondviewcontroller.correctAnswers = [NSNumber numberWithInt:antalratt];

传递antalratt的值

到secondviewcontroller。然后设置labeltext

答案 2 :(得分:1)

方法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

方法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
}

方法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
}

答案 3 :(得分:0)

//     view1.h

@interface view1 : UIView{

NSString *passingVariable;

}

@property (nonatomic, strong) NSString *passingVariable;

@end

//

view1.m

@synthsize passingVariable;

@implementation view1

@end

//在另一个视图中

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