我正在写我的第一个有罪的iPhone应用程序。我的登录界面已经准备好I thinK了。我在MainWindow中实现了一个用于登录凭据的视图,并将所有UI添加到该视图(文本框等)。
这次又是什么?
我想要检查我的凭据,如果可以,我将从主窗口终止登录视图,并显示包含所有信息的主视图。
怎么办?
我是否可以轻松地在loginviewcontroller中添加按钮事件,检查那里的logindata,并访问主窗口,并从那里在视图本身上无问题地终止?或者我必须在主窗口的视图中声明button-event并在那里做所有的事情吗?
我的代码:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andDelegate:(id)delegate
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(self.view.frame.size.width / 2 - 55.0f, self.view.frame.size.height / 2 + 85.0f
, 110.0f, 35.0f);
[button setTitle:@"title" forState:UIControlStateNormal];
[button addTarget:delegate action:@selector(yourMethodInMainWindow:) forControlEvents: UIControlEventTouchDown];
[self.view addSubview:button];
}
return self;
}发布于 2011-08-02 20:12:12
发布于 2011-08-02 18:43:24
在视图控制器中:
//maybe you will need to have more arguments added to you init method
//for instance you might be using -(id)initWithNibName:bundle: right now
//then just add delegate to it like -(id)initWithNibName:bundle:andDelegate:
//just to clarify: delegate = reference to class you want to call a method in
-(id)initMethod:(id)delegate {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.button.frame = CGRectMake(self.view.frame.size.width / 2 - 55.0f, self.view.frame.size.height / 2 + 85.0f
, 110.0f, 35.0f);
[button setTitle:@"title" forState:UIControlStateNormal];
[button addTarget:delegate action:@selector(yourMethodInMainWindow:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}在使用mainWindow的课堂上:
-(void)yourMethodInMainWindow:(id)sender {
//do whatever you want to do.
}这是以编程方式设置它的一种方式。如果你使用的是nib,那么你将在你的.h-file och类型- (IBAction)buttonClicked:(id)sender中定义一个方法,然后在接口构建器中连接这些方法。
我猜你知道如何推动一个新的观点。例如,可以用presentModalViewController:animated:来完成。所有这些都已经在stackoverflow和google上实现了。
答案here可能会告诉您关于访问UIApplication以及访问窗口需要了解的内容。
发布于 2011-08-02 21:08:46
您应该使用导航控制器在不同视图之间进行转换。
首先,将导航控制器插入到MainWindow.xib中
然后,在该导航控制器中加载LoginViewController。LoginviewController将是放置您的登录id、密码和登录btn的视图。
现在,对于login btn操作,编写一个方法,
- (IBAction) loginBtnPressed : (id) sender{
MainViewController *newObject = [MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
[self.navigationController pushViewController:newObject animated:YES];
}编写此方法后,只需在接口构建器中将此方法附加到.xib文件中的btn即可。
https://stackoverflow.com/questions/6910519
复制相似问题