Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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
Ios 如何自定义ui项并将其应用于所有ViewController?_Ios_User Interface_Uikit_Iboutlet - Fatal编程技术网

Ios 如何自定义ui项并将其应用于所有ViewController?

Ios 如何自定义ui项并将其应用于所有ViewController?,ios,user-interface,uikit,iboutlet,Ios,User Interface,Uikit,Iboutlet,我有一个UITextField,它是我在firstViewController中定制的。 现在我不希望在其他ViewController上有相同的行为。 是否要导入IBOutlet上的所有属性?为什么不定义一个基本视图控制器,然后从中派生所有视图控制器 @interface MyBaseCustomViewController : UIViewController ... @property(...) UITextField* ... ... @end @interface MyOther

我有一个UITextField,它是我在firstViewController中定制的。 现在我不希望在其他ViewController上有相同的行为。
是否要导入IBOutlet上的所有属性?

为什么不定义一个基本视图控制器,然后从中派生所有视图控制器

@interface MyBaseCustomViewController : UIViewController
 ...
@property(...) UITextField* ...
 ...
@end

@interface MyOtherCustomViewController : MyBaseCustomViewController
 ...

为什么不定义一个基本视图控制器,然后从中派生所有视图控制器

@interface MyBaseCustomViewController : UIViewController
 ...
@property(...) UITextField* ...
 ...
@end

@interface MyOtherCustomViewController : MyBaseCustomViewController
 ...

是的,您可以创建UITextField的子类,该子类将具有您在viewcontroller中作为子类中的函数所做的所有自定义代码

比如说

- (void) viewDidLoad
{
    //UITextField *textField;
    //change color, background, size etc..
}
现在创建一个名为UICustomTextField的新类,该类派生自UITextField 在此类中创建一个方法:

//in UICustomTextField.m
- (void) doCustomModifications
{
    self.stuff = custom stuff;
    other custom stuff
    etc...
}
在代码中调用doCustomModifications

- (void)viewDidLoad
{
    [customTextField doCustomModifications];
}

是的,您可以创建UITextField的子类,该子类将具有您在viewcontroller中作为子类中的函数所做的所有自定义代码

比如说

- (void) viewDidLoad
{
    //UITextField *textField;
    //change color, background, size etc..
}
现在创建一个名为UICustomTextField的新类,该类派生自UITextField 在此类中创建一个方法:

//in UICustomTextField.m
- (void) doCustomModifications
{
    self.stuff = custom stuff;
    other custom stuff
    etc...
}
在代码中调用doCustomModifications

- (void)viewDidLoad
{
    [customTextField doCustomModifications];
}

简单地创建您自己的Customclass并在构造函数中设置属性

@interface CustomTextField : UITextField

@end


@implementation CustomTextField

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
      //Customize here
      self.autocapitalizationType = UITextAutocorrectionTypeDefault;
      self.text = @"Blub";
      ....
    }
    return self;
}
@end
如果要创建新的Textfield,请使用自定义类创建对象:

CustomTextField *field = [[CustomTextField alloc] initWithFrame: ...];

简单地创建您自己的Customclass并在构造函数中设置属性

@interface CustomTextField : UITextField

@end


@implementation CustomTextField

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
      //Customize here
      self.autocapitalizationType = UITextAutocorrectionTypeDefault;
      self.text = @"Blub";
      ....
    }
    return self;
}
@end
如果要创建新的Textfield,请使用自定义类创建对象:

CustomTextField *field = [[CustomTextField alloc] initWithFrame: ...];

你是用代码还是用xib来定制的?我用代码:)基本上我用了一些方法来做搜索建议,还有一个costum背景。你是用代码还是用xib来定制的?我用代码:)基本上我用了一些方法来做搜索建议,还有一个costum背景。哼,你是说像模板一样?看起来很可怕。我对ios还不熟悉,所以我还在深入研究它。实际上,它不是一个模板,只是一个基类。也许,如果你发布你的代码和一个你想要达到的目标的例子,这会有所帮助……哼,你是说像一个模板?看起来很可怕。我对ios还不熟悉,所以我还在深入研究它。实际上,它不是一个模板,只是一个基类。也许,如果你发布你的代码和一个你想要达到的目标的例子,这可能会有所帮助……欢迎你,如果有用的话,别忘了投票或接受答案:)欢迎你,如果有用的话,别忘了投票或接受答案:)太棒了。谢谢你的帮助!令人惊叹的。谢谢你的帮助!