Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c ios的文本字段_Objective C_Xcode_Uitextfield - Fatal编程技术网

Objective c ios的文本字段

Objective c ios的文本字段,objective-c,xcode,uitextfield,Objective C,Xcode,Uitextfield,我对iOS和xcode非常陌生。我想在不使用nib文件或情节提要的情况下创建文本字段。我搜索了答案,但到处都是令人困惑的地方,或者他们使用了nib文件。我已经创建了按钮,它工作得很好,但是对于文本字段,我面临着一些问题。这是我的viewcontroller.m请帮忙 #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)loadView {

我对iOS和xcode非常陌生。我想在不使用nib文件或情节提要的情况下创建文本字段。我搜索了答案,但到处都是令人困惑的地方,或者他们使用了nib文件。我已经创建了按钮,它工作得很好,但是对于文本字段,我面临着一些问题。这是我的
viewcontroller.m
请帮忙

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)loadView {


self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.backgroundColor = [UIColor purpleColor];


UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UITextField *textfield;


button.frame = CGRectMake(10 ,350 , 300, 40);
button1.frame = CGRectMake(10 ,50 , 300, 40);
textfield.frame = CGRectMake(10, 100, 200, 30);


[button setTitle:@"Change screen to green!" forState:UIControlStateNormal];
[button1 setTitle:@"click screen to red!" forState:UIControlStateNormal];

[button addTarget:self action:@selector(buttonPressed)     forControlEvents:UIControlEventTouchUpInside];
[button1 addTarget:self action:@selector(buttonPressed1) forControlEvents:UIControlEventTouchUpInside];


[self.view addSubview:button];
[self.view addSubview:button1];
[self.view addSubview:textfield];
}
-(void)buttonPressed {
self.view.backgroundColor = [UIColor greenColor];

}
-(void)buttonPressed1 {
self.view.backgroundColor = [UIColor redColor];

}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end
感谢和问候

请使用此:

UITextField *textfield = [[UITextField alloc] init];
插入

UITextField *textfield;

UITextField*textfield只是克里特对象引用而不是对象。

您只是声明了
UITextField
,而不是分配。您必须先分配它,然后设置
&将其添加到
视图中-

UITextField *txt = [[UITextField alloc] init];
txt.frame = CGRectMake(10, 100, 200, 30);
txt.delegate = self;
[self.view adsubview:txt];

它解决了您的问题。

如果您像答案所建议的那样添加了alloc init,您的文本字段实际上在屏幕上,但由于它没有边框或文本,您无法看到它。尝试添加此行,然后您将看到:

textfield.borderStyle = UITextBorderStyleRoundedRect;
首次使用

UITextField *textfield = [[UITextField alloc] init];
而不是

UITextField *textfield;
然后设置textfield的背景颜色,因为背景颜色默认为clearColor

textfield.backgroundColor   =   [UIColor whiteColor];
或使用

textfield.borderStyle = UITextBorderStyleRoundedRect;
获取带白色边框的文本字段

UITextField *textfield =[[UITextField alloc] init];
 textfield.frame = CGRectMake(10, 100, 200, 30); 
textfield.delegate = self; [self.view addSubview:textfield];
[self.view bringSubViewToFront:textfield];

可能会解决问题

谢谢@CRDave我这么做了,但我仍然看不到屏幕上的文本字段。UITextField*textfield=[[UITextField alloc]init];textfield.frame=CGRectMake(10100200,30);textfield.delegate=self;[self.view addSubview:textfield];}-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{return(interfaceOrientation!=UIInterfaceOrientationSpatureUpsideDown);}@endi只想创建一个文本字段。这是我的.m文件中的内容,但我仍然看不到屏幕上的文本字段。嗯,问题似乎是其他问题。使用.h和.m文件代码编辑您的问题。谢谢您的帮助。刚刚添加了这一行textfield.borderStyle=UITextBorderStyleRoundedRect;它现在可以工作了。它在那里,但我应该给边界风格。这似乎解决了问题@shanusinghthanks@Girish。但当我使用委托时,它会向我发出警告,我无法在屏幕上看到textfield。在.h文件中设置UITextFieldDelegate,如@interface SearchWineViewController:UIViewController&在.m文件中实现委托方法。同时设置适当的框架来显示文本字段。我在.h文件中设置了委托,在.m文件中实现了委托,但我仍然无法在屏幕上看到文本字段。请尝试设置适当的框架。