正如标题中所写,我有一个属性UITextField.我把它UITextField加到了UIView.
如果我有一个强大的指针,UITextField出现,
如果我有一个弱指针,UITextField则不会出现.
当我有一个弱指针时出了什么问题?我做了同样的UIButton事情,然后它实际上出现(强指针和弱指针).
这是一些代码:
CreateCategoryView.h
@interface CreateCategoryView : UIView
@property (weak, nonatomic) UITextField *nameTextField;
@end
Run Code Online (Sandbox Code Playgroud)
CreateCategoryView.m
@implementation CreateCategoryView
- (id)initWithFrame:(CGRect)frame andParent {
self = [super initWithFrame:frame];
if (self) {
self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(5, 30, 310, 25)];
self.nameTextField.borderStyle = UITextBorderStyleRoundedRect;
self.nameTextField.textColor = [UIColor blackColor];
self.nameTextField.backgroundColor = [UIColor whiteColor];
[self addSubview:self.nameTextField];
}
return self;
}
@end
Run Code Online (Sandbox Code Playgroud) 我通常创建initialize()用于设置on-click侦听器的函数和函数,然后从Activity中调用这些函数onCreate().当我打电话给someView.setOnClickListener(...)某个功能时,我应该检查一下是否someView == null?我知道,它!= null,但我需要一个关于编码风格的建议.哪种是最佳做法?
这是一个例子:
...
public class SomeActivity extends Activity
{
private ImageButton someButton;
private Intent someIntent;
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.add_alarm);
initialize();
setSomeButtonHandler();
}
public void initialize()
{
someButton = (ImageButton) findViewById(R.id.someButton);
}
public void setSomeButtonHandler()
{
if(someButton != null) //Should I check this?
{
someIntent = new Intent(SomeActivity.this, SomeButtonActivity.class);
someButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
startActivity(someIntent);
}
});
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用此代码
-(void)gotoInformationViewController:(id)sender
{
MoreViewController *moreViewController = [[MoreViewController alloc] init];
[self.navigationController pushViewController:moreViewController animated:YES];
[moreViewController release];
}
Run Code Online (Sandbox Code Playgroud)
推动一个MoreViewController(这是UITableViewController),但它UINavigationBar在推动时没有,只是UITableView在整个屏幕上.
我使用故事板生成了一个新的选项卡式应用程序.
到目前为止我有
TabBarController - > FirstViewController - > SecondViewController - > ModalViewController
我正在尝试在显示tabBarController之前打开模态视图.我在AppDelegate.m上添加了以下代码
showModalView 被称为 application:didFinishLaunchingWithOptions:;
- (void)showModalView
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
GSLoginViewController *loginView = [storyboard instantiateViewControllerWithIdentifier:@"loginView"];
[loginView setModalPresentationStyle:UIModalPresentationFullScreen];
[self.window.rootViewController presentViewController:loginView animated:YES completion:NULL];
}
Run Code Online (Sandbox Code Playgroud)
在这里输出我:
Warning: Attempt to present <ModalViewController: 0x93670d0> on
<UITabBarController: 0x935d170> whose view is not in the window hierarchy!
Run Code Online (Sandbox Code Playgroud)