Gab*_*ter 2 string iphone xcode objective-c ios
我一直在使用objective-c做一件事,但我是这种语言的初学者,这就是我所做的:
有一个名为"firstviewclass"的类,它控制着我的第一个视图,在这个视图中有一个用户输入数字的文本字段.textfield在firstviewclass.h中,名为"setNumber".还有另一个名为"secondviewclass"的类控制着第二个视图,在这个视图中有一个标签位于secondviewclass.h中,我希望这个标签能够重现用户放在文本域中的值.第一个观点.我的代码:
firstviewclass.h:
#import <UIKit/UIKit.h>
@interface firstviewclass : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *setNumber;
- (IBAction)gotonextview:(id)sender;
@end
Run Code Online (Sandbox Code Playgroud)
secondviewclass.h:
#import <UIKit/UIKit.h>
#import "firstviewclass.h"
@interface secondviewclass : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *labelthatrecivesthetextfieldvalue;
@end
Run Code Online (Sandbox Code Playgroud)
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 = string; //here is where that error appears "incompatible pointer types assigning to 'UILabel *__weak' to 'NSString *__strong'"
}
@end
Run Code Online (Sandbox Code Playgroud)
我已经改变了你告诉我改变的内容,但当我在模拟器上测试它在文本字段中放置的任何数字时,它在标签中显示为0 ...我真的不知道该怎么做!
Rui*_*res 11
它应该是:
labelthatrecivesthetextfieldvalue.text = string;
Run Code Online (Sandbox Code Playgroud)
引擎盖下:
在你的xib实例化之后UILabel,你使用IBOutlet关键字得到它的引用,然后你尝试使用UILabel指针指向分配和实例化为a的内存空间NSString,这就是你遇到这个问题的原因.您可能也确实收到了关于您尝试执行的任务的警告:
labelthatrecivesthetextfieldvalue = string;
Run Code Online (Sandbox Code Playgroud)
您应该始终尝试修复警告.
你想要什么:
您只想UILabel显示NSString刚创建的内容,为此您应该使用UILabel'stext属性.