嘿家伙我正在制作一个有2个视图的应用程序。第一个视图是用户输入数字的位置,当他们点击计算按钮时,它将它们带到一个新的视图,其中答案显示在文本框中。该计算由不同的文件处理。 “calc.h”和“calc.m”Multiview应用程序。无法更改文本字段值
这是主要的视图文件
#import <UIKit/UIKit.h>
@interface test2ViewController : UIViewController {
IBOutlet UITextField *num1; //input number 1
IBOutlet UITextField *num2; // input number2
}
@property (nonatomic,retain) UITextField *num1;
@property(nonatomic, retain)UITextField *num2;
-(IBAction)calculate:(id)sender; //the calculate button
-(IBAction)exit:(id)sender;
@end
答案被显示在不同的视图(其中,用户输入的数字的视图)示出若用户点击计算按钮。下面是该代码!
#import "test2ViewController.h"
#import "answer.h"
#import "calculate.h"
@implementation test2ViewController
@synthesize num1,num2;
-(IBAction)calculate:(id)sender{
calculate *testclass = [[calculate alloc]init];
answer *view = [[answer alloc]initWithNibName:nil bundle:nil];
[self presentModalViewController:view animated:YES];
int i = [[num1 text] intValue];
testclass.number1 = i;
int j = [[num2 text] intValue];
testclass.number2 = j;
[testclass calc];
}
显示的答案
#import <UIKit/UIKit.h>
@interface answer : UIViewController {
IBOutlet UITextField *text;//textfield to display answer
}
@property(nonatomic,retain) UITextField *text;
-(IBAction)back:(id)sender;
@end
视图
“的.h” 文件
“的.m” 文件
#import "answer.h"
#import "test2ViewController.h"
#import "calculate.h"
@implementation answer
@synthesize text;
-(IBAction)back:(id)sender{
test2ViewController *view = [[test2ViewController alloc]initWithNibName:nil bundle:nil];
[self presentModalViewController:view animated:YES];
}
下一是客观C类文件,做计算。它有一个函数,它的计算和这里是“.H”和“.M”文件 “.H”
#import <Foundation/Foundation.h>
@interface calculate : NSObject {
int number1;
int number2;
NSString *ans;
}
@property int number1;
@property int number2;
@property (nonatomic , retain) NSString *ans;
-(void)calc;
@end
“.M”
#import "calculate.h"
#import "answer.h"
#import "test2ViewController.h"
@implementation calculate
@synthesize number1,number2,ans;
-(void)calc{
int i = number1 + number2;
answer *ans1 = [[answer alloc]init];
ans = [NSString stringWithFormat:@"%d",i];
ans1.text.text = ans;
}
@end
所以你可以看到上面的函数计算答案..将其输入到一个字符串中并将其设置为文本字段。但文本字段不显示任何东西..所以这里的问题是,即使我从它创建了一个对象,我无法访问文本字段....
我试过你的方法,它似乎没有工作..我用我的代码编辑过这篇文章。 – cyberbemon
哦,我明白你想做什么。虽然它很多没有多少意义。所以最终答案的变量是'ans1'? – Jacob
ans1是从answer.h创建的一个对象,在answer.h中有一个名为text的UItextfield。'ans1.text.text = ans'..将UItextfield的文本设置为ans(这是最终答案!)xD多数民众赞成在我想要做的..但由于某种原因,它不会wrk ..在坚果壳这是我想要做的..输入数字view1做一个不同的文件计算。当按钮被按下时在不同的视图中显示答案! – cyberbemon