0

我一直在这里使用objective-c做一件事,但我是这门语言的初学者,这就是我所做的:

有一个名为“firstviewclass”的类,它在我的第一个视图的控制中,在这个视图中有一个用户输入数字的文本字段。文本字段位于名为“setNumber”的 firstviewclass.h 中。还有另一个名为“secondviewclass”的类控制第二个视图,在这个视图中有一个标签位于 secondviewclass.h 中,我希望这个标签接收用户在文本字段中输入的值第一个视图,但是当我在 iOS 模拟器上对其进行测试时,我在文本字段中输入的任何数字都会在标签中显示为 0……我真的不知道该怎么做!

我的代码:

第一视图类.h:

#import <UIKit/UIKit.h>

@interface firstviewclass : UIViewController

@property (strong, nonatomic) IBOutlet UITextField *setNumber;

- (IBAction)gotonextview:(id)sender;

@end

secondviewclass.h:

#import <UIKit/UIKit.h>
#import "firstviewclass.h"

@interface secondviewclass : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *labelthatrecivesthetextfieldvalue;

@end

secondviewclass.m:

#import "secondviewclass.h"
#import "firstviewclass.h"

@implementation secondviewclass
@synthesize labelthatrecivesthetextfieldvalue;


-(void)viewDidLoad{
    [super viewDidLoad];

    firstviewclass *object = [[firstviewclass alloc] init];

    NSString *string = [[NSString alloc] initWithFormat:@"%i", [object.setNumber.text intValue]];

    labelthatrecivesthetextfieldvalue.text = string;

}

@end
4

3 回答 3

1

在 secondviewclass 的 viewDidLoad 中,您正在创建 firstviewclass 的新实例,然后访问标签的属性。由于您没有将其设置为任何值,因此它将始终返回零。

您可以发布 gotonextview 方法的实现吗?

编辑:

我认为您需要从第一个视图控制器设置标签属性。所以在你的 gotonextview 方法中,添加这一行:

secondviewclass.labelthatrecivesthetextfieldvalue.text = self.setNumber.text;

我承认我对你的命名方案有点迷茫,但这可能会奏效。

于 2012-07-15T21:41:06.133 回答
1

How are you initiating the setting of the label? From viewDidLoad? If so, then 0 would be correct because you have not set object.setNumber.text to anything. Also, you aren't carrying over the data from your first viewController. You need to let the second viewController know what the number is. Try setting this value in your NSUserDefaults, then in your second viewController, load that value from your defaults.

于 2012-07-15T21:43:27.150 回答
1

First of all, I would strongly recommend not naming a property or instance variable setSomething. It will cause headaches for anyone reading your code because it will always look like you're trying to call a setter. Also, please do capitalize your class names.

Your actual problem is that in viewDidLoad you're creating an instance of firstviewclass, and then trying to get the value from setNumber. That is before the user had any chance to enter anything.

Also, the setNumber outlet in firstviewclass will probably be going nowhere anyway, since you're instantiating that class yourself, instead of loading the NIB.


Edit (ah, Storyboard, d'oh):

For Storyboard, you need to pass the setNumber text field's value to the second view.

First of all, remove the firstviewclass *object = [[firstviewclass alloc] init]; line.

Then, in your first view controller's prepareForSegue method, you can pass the value of the setNumber text field to a property in your your second view controller, and use it from there (e.g. in configureView).

I recommend working through Apple's Storyboard tutorial, it shows exactly what you need to do, step by step. The step you're having issues with right now, passing data to your next view controller, is here.

于 2012-07-15T21:44:11.533 回答