2012-02-18 47 views
0

我想构建一个由一组值组成的计算器,它们是常数,一些值是变量。将存储在IBAction中的值传递给下一个Nib UITextField。

该计算器将首先从一个UIButton启动,我想在UIButton中使用IBAction存储常数值。然后通过触摸该按钮将在UITextField上以恒定值显示启动计算器。

我的问题是,我可以输入什么代码来将常量值存储在IBAction的代码中,并将其显示在下一个nib文件UITextField中。以下是我目前的代码。

很多谢谢。

- (IBAction)runningtrack:(id)sender { 

int length = 400; 
int width = 30; 

track *myView3 =[[track alloc] initWithNibName:nil bundle:nil]; 
     [myView3 setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; 
     [self presentModalViewController:myView3 animated:YES]; 
    } 

回答

1

哟可以使用委托方法或容易(不是首选)我们NSUSerDefaults。如果yu使用NSUserDefaults,你的代码将是这样的。

- (IBAction)runningtrack:(id)sender { 

int length = 400; 
int width = 30; 
NSNumber *theNumber = [NSNumber numberWithInt:<lenght&with or which int value you will save>];//NSUSerDefaults saves property list objects, so convert int to NSNumber 
NSUserDefaults *savedValue=[NSUserDefaults standartuserdefault]; 
[savedValue setObject:theNumber forKey:@"myConst"]; 
track *myView3 =[[track alloc] initWithNibName:nil bundle:nil];// by the way i don't think initWithNibName:nil give a proper response, it must crash 
     [myView3 setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; 
     [self presentModalViewController:myView3 animated:YES]; 
    } 

在其他类(其中有要显示达价值的.xib)

NSNumber *yourValue=[[NSUserDefaults standartuserdefault] objectForKey:@"myConst"]; 
UITextView *yourTextView=... 
yourTextView.text=yourValue; 
2

您需要track类申报财产或进行自定义init()方法。

  1. 物业

    track *myView3 =[[track alloc] initWithNibName:nil bundle:nil]; 
    track.valueForTextField = width; //valueForTextField is a int property in track class 
    
  2. 自定义的init:

    track *myView3 =[[track alloc] initWithNibName:nil bundle:nil andMyValue:width]; 
    
1

你需要编写以下自定义init()方法。

track *myView3 =[[track alloc] initWithNibName:nil bundle:nil customLength:400 customWidth:300];

您必须遵守使用上面的代码之前,下面的步骤。

  1. 在track.h头文件中,添加自定义的init方法声明。

    - (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle customLength:(int) customWidth:(int);

  2. 在track.m文件,添加自定义的方法定义。

    - (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle customLength:(int) customWidth:(int) { /*You can use the customLength and customWidth here */}

相关问题