您的位置:首页 > 其它

iphone开发我的新浪微博客户端-用户登录账号删除篇(1.6)

2011-08-31 17:04 441 查看


在上一篇中我们完成账号的添加的功能,本篇就实现账号删除的功能,完成的具体效果看上(图5),当点击删除按钮的时候出现一个弹出对话框显示是否删除当前默认选择的用户,这个显示功能跟上一篇的添加几乎没有什么区别了,同样这里的实现依旧是基于自定义组件UIDialogWindow。当进一步点击确定按钮时,从Sqlite库中把当前选中的账号从表中删除,然后更新默认选择账号,从剩下的用账号录中选择第一个账号作为默认选中,如果剩下的账号记录已经为空那么页面跳转到前面实现的用户授权功能页面引导用户重新进行用户授权操作。如果点击的是取消按钮那么就关闭弹出对话框什么也不进行操作。

一、首先按照UIDialogWindow的用法先新建名为DelConfirmViewController的UIViewController subclass类型的类文件,新进的时候记得勾上With XIB user interface选项。
二、打开上一步新建的DelConfirmViewController.h文件,声明一个UITextView用来显示文字和两个UIButton按钮(确定按钮和取消按钮),delegate和onClick就不用说了,前面提到好几次了,还有确定按钮事件和取消按钮事件。具体代码如下:

#import <UIKit/UIKit.h>
#import "Global.h"

@interface DelConfirmViewController : UIViewController {

id delegate;
SEL onClick;

IBOutlet UITextView *textView;
IBOutlet UIButton *okBtn;
IBOutlet UIButton *cancelBtn;
}

@property (nonatomic,retain)IBOutlet UITextView *textView;
@property (nonatomic,retain)IBOutlet UIButton *okBtn;
@property (nonatomic,retain)IBOutlet UIButton *cancelBtn;

-(void)setDelegate:(id)aDelegate onClick:(SEL)aOnClick;
-(IBAction)okDel:(id)sender;
-(IBAction)cancelDel:(id)sender;

@end


三、双击DelConfirmViewController.xib文件,在IB中打开从Library中拖相应的组件到View上并且设置相应的属性,还需要适当的设置view的尺寸大小以及view背景透明,具体如下图:



完成布局后还需要完成相应的连接工作了,具体如下图:



四、完成DelConfirmViewController.xib设计后打开DelConfirmViewController.m完成如下代码:

-(void)setDelegate:(id)aDelegate onClick:(SEL)aOnClick
{
delegate=aDelegate;
onClick=aOnClick;

}

-(IBAction)okDel:(id)sender
{
if (delegate) {
[delegate performSelector:onClick withObject:@"ok"] ;
}
}

-(IBAction)cancelDel:(id)sender
{
if (delegate) {
[delegate performSelector:onClick withObject:@"cancel"] ;
}
}
-(void)setDelegate:(id)aDelegate onClick:(SEL)aOnClick
{
delegate=aDelegate;
onClick=aOnClick;

}

-(IBAction)okDel:(id)sender
{
if (delegate) {
[delegate performSelector:onClick withObject:@"ok"] ;
}
}

-(IBAction)cancelDel:(id)sender
{
if (delegate) {
[delegate performSelector:onClick withObject:@"cancel"] ;
}
}


五、接下来就是在LoginViewController调用DelConfirmViewController进行显示,首先在LoginViewController.h添加声明DelConfirmViewController。接下来点击删除按钮调用DelConfirmViewController进行显示的代码已经在前面一篇的步骤六中完成了,有没有发现到这里为止跟前面的一片几乎一模一样了没有什么区别。
六、接下来开始就是不同的地方了,点击删除按钮后调用[self showDelConfirm],现在完成showDelConfirm方法代码如下:

//显示删除帐号确认框
-(void)showDelConfirm
{
delConfirmViewController=[[DelConfirmViewController alloc] init];

dialog=[[UIDialogWindow alloc]initWithView:delConfirmViewController.view];
[delConfirmViewController setDelegate:self onClick:@selector(confirmDel:)];
NSString *msg=[NSString stringWithFormat:@"您确定要删除名为“%@”的帐号?",selectViewController.nameLabel.text];
[[delConfirmViewController textView] setText:msg];
[dialog show];

}


七、在上面的代码中为确定按钮设置了事件方法confirmDel,代码如下:

//确认删除帐号
-(void)confirmDel:(NSString *)txt
{
[[[[UIApplication sharedApplication] windows] objectAtIndex:0] makeKeyAndVisible];

[dialog close];
if (txt==@"ok") {
Sqlite *sqlite=[[Sqlite alloc] init];
if([sqlite delUser:selectViewController.nameLabel.text])
{
[self showUserInfo];
}
}
}


上面的方法中调用了Sqlite中的delUser方法从数据库中把选中的账号,删除完成后调用showUserInfo方法更新账号显示,这个方法前面的文章中已经提供了。
到处本篇的功能也就完成了,请继续关注下一篇。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: