我的应用程序有一个名为 的变量int antalratt
,它是该视图中正确答案的数量。现在我想将该变量传递给下一个视图,我想在其中获得要显示的正确答案的数量!我知道如何为标签文本获取整数!
int antalratt
写在 中,firstviewcontroller.m
我如何使它“全局”,以便我可以在中使用它secondviewcontroller
?
提前致谢!
我的应用程序有一个名为 的变量int antalratt
,它是该视图中正确答案的数量。现在我想将该变量传递给下一个视图,我想在其中获得要显示的正确答案的数量!我知道如何为标签文本获取整数!
int antalratt
写在 中,firstviewcontroller.m
我如何使它“全局”,以便我可以在中使用它secondviewcontroller
?
提前致谢!
在 secondviewcontroller.h 的公共接口中创建一个变量
@property (nonatomic, strong) NSNumber *correctAnswers;
在 .m 中合成它,然后在 firstviewcontroller 中传递 antalratt 的值secondviewcontroller.correctAnswers = [NSNumber numberWithInt:antalratt];
到第二视图控制器。然后设置标签文本
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;
和文件名.m
@synthesize audio;
-(void) ViewDidLoad
{
NSLog(@"Audio = %@",audio); // if ur variable is integer declare %d in nslog.
}
就这样
方法一:
根视图控制器
-(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];
}
第一视图控制器
@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
方法二(AppDelegate)
应用委托
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
int antalratt;
}
@property(nonatomic ,assign) int antalratt;
根视图控制器
-(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];
}
第一视图控制器
- (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)
根视图控制器
-(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]; }
第一视图控制器
- (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
}
// 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