Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >当用户名/密码对Twitter不正确时,如何显示操作表?

当用户名/密码对Twitter不正确时,如何显示操作表?
EN

Stack Overflow用户
提问于 2010-01-04 14:42:10
回答 2查看 928关注 0票数 0

我希望我的应用程序在用户按下"Tweet"-Button并且用户名或密码错误时显示一个操作表。对于我的Twitterfunction,我使用Brandon Trebitowski的TwitterRequest.m/h。如果一切正常,用户名/密码正确,这在我的应用程序中就会发生:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
        TwitterRequest * t = [[TwitterRequest alloc] init];
        (...);
        [t statuses_update:twittermessage.text delegate:self requestSelector:@selector(status_updateCallback:)];

        loadingActionSheet = [[UIActionSheet alloc] initWithTitle:@"Posting to Twitter..." delegate:nil 
                                                cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
        [loadingActionSheet showInView:self.view];
    }


    - (void) status_updateCallback: (NSData *) content {
        [loadingActionSheet dismissWithClickedButtonIndex:0 animated:YES];
        [loadingActionSheet release];
        NSLog(@"%@",[[NSString alloc] initWithData:content encoding:NSASCIIStringEncoding]);
    }

但是,当用户名/密码错误时,我如何显示其他操作表?下面是TwitterRequest.m:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#import "TwitterRequest.h"

@implementation TwitterRequest

@synthesize username;
@synthesize password;
@synthesize receivedData;
@synthesize delegate;
@synthesize callback;
@synthesize errorCallback;

-(void)friends_timeline:(id)requestDelegate requestSelector:(SEL)requestSelector{
    isPost = NO;
    // Set the delegate and selector
    self.delegate = requestDelegate;
    self.callback = requestSelector;
    // The URL of the Twitter Request we intend to send
    NSURL *url = [NSURL URLWithString:@"http://twitter.com/statuses/friends_timeline.xml"];
    [self request:url];
}

-(void)statuses_update:(NSString *)status delegate:(id)requestDelegate requestSelector:(SEL)requestSelector; {
    isPost = YES;
    // Set the delegate and selector
    self.delegate = requestDelegate;
    self.callback = requestSelector;
    // The URL of the Twitter Request we intend to send
    NSURL *url = [NSURL URLWithString:@"http://twitter.com/statuses/update.xml"];
    requestBody = [NSString stringWithFormat:@"status=%@",status];
    [self request:url];
}

-(void)request:(NSURL *) url {
    theRequest   = [[NSMutableURLRequest alloc] initWithURL:url];

    if(isPost) {
        NSLog(@"ispost");
        [theRequest setHTTPMethod:@"POST"];
        [theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [theRequest setHTTPBody:[requestBody dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
        [theRequest setValue:[NSString stringWithFormat:@"%d",[requestBody length] ] forHTTPHeaderField:@"Content-Length"];
    }

    theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (theConnection) {
        // Create the NSMutableData that will hold
        // the received data
        // receivedData is declared as a method instance elsewhere
        receivedData=[[NSMutableData data] retain];
    } else {
        // inform the user that the download could not be made
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    //NSLog(@"challenged %@",[challenge proposedCredential] );

    if ([challenge previousFailureCount] == 0) {
        NSURLCredential *newCredential;
        newCredential=[NSURLCredential credentialWithUser:[self username]
                                                 password:[self password]
                                              persistence:NSURLCredentialPersistenceNone];
        [[challenge sender] useCredential:newCredential
               forAuthenticationChallenge:challenge];

    } else {
        [[challenge sender] cancelAuthenticationChallenge:challenge];
        // inform the user that the user name and password
        // in the preferences are incorrect
        NSLog(@"Invalid Username or Password");
    }

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // this method is called when the server has determined that it
    // has enough information to create the NSURLResponse

    // it can be called multiple times, for example in the case of a
    // redirect, so each time we reset the data.
    // receivedData is declared as a method instance elsewhere
    //[receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    //NSLog([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    // append the new data to the receivedData
    // receivedData is declared as a method instance elsewhere
    [receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{
    // release the connection, and the data object
    [connection release];
    // receivedData is declared as a method instance elsewhere
    [receivedData release];

    [theRequest release];

    // inform the user
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);

    if(errorCallback) {
        [delegate performSelector:errorCallback withObject:error];
    }
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // do something with the data

    if(delegate && callback) {
        if([delegate respondsToSelector:self.callback]) {
            [delegate performSelector:self.callback withObject:receivedData];
        } else {
            NSLog(@"No response from delegate");
        }
    } 

    // release the connection, and the data object
    [theConnection release];
    [receivedData release];
    [theRequest release];
}

-(void) dealloc {
    [super dealloc];
}


@end

对于这个愚蠢的问题,我很抱歉,但我只学习了一周的Objective-C和一般编程,我不知道如何正确地从我的ViewController与其他类进行交互。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-01-04 15:24:37

要实现动作单,您必须首先在头文件中实现UIActionSheetDelegate (在<>之间的@接口定义中包含UIActionSheetDelegate )。

在您的代码中,您将显示动作单并捕获按钮按下时的动作。要显示操作表,请执行以下操作:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
UIActionSheet *actionSheet = [[UIActionSheet alloc]
                              initWithTitle:@"Choose one, por favor"
                              delegate:self
                              cancelButtonTitle:@"Cancel"
                              destructiveButtonTitle:nil
                              otherButtonTitles:@"Save Favorite", @"Email", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
actionSheet.cancelButtonIndex = 2;
[actionSheet showInView:self.view];
[actionSheet release];

要对按钮按下操作,请使用以下方法:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    // the user clicked one of the OK/Cancel buttons
    NSLog(@"The button index is: %i", buttonIndex);

    switch (buttonIndex) {
        case 0:
            NSLog(@"Button 0");
            [self saveNew];
            break;
        case 1:
            NSLog(@"Button 1");
            [self sendEmail];
            break;
        case 2:
            NSLog(@"Button 2");
            break;
        default:
            break;
    }
}

您的另一个选择是使用警报--对于不正确的用户名/密码,这可能是最好的选择。警报是一个显示在屏幕中央的模式框。要实现警报,请在头文件中实现UIAlertViewDelegate。

示例警报代码如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
UIAlertView *alert;
alert = [[UIAlertView alloc] initWithTitle:@"Ouch!"
  message:@"Your message is placed here"
  delegate:self 
  cancelButtonTitle:@"OK" 
  otherButtonTitles: nil];
[alert show];   
[alert release];
票数 2
EN

Stack Overflow用户

发布于 2010-01-04 15:30:38

如果您希望在用户收到身份验证请求时做出反应,则需要在didReceiveAuthenticationChallenge内部工作。

具体来说,就在这条线的正上方:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
newCredential=[NSURLCredential credentialWithUser:[self username]
                                             password:[self password]
                                          persistence:NSURLCredentialPersistenceNone];

是您希望从用户处获取用户名和密码的位置。

如果您希望处理身份验证失败,则需要利用previousCountFailure if语句中的'else‘子句。

具体地说,在这一行之后是您想要告诉用户他们失败的地方:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[[challenge sender] cancelAuthenticationChallenge:challenge];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1999930

复制
相关文章
Vue自定义组件-属性使用
自定义组件Button <template> <button> <span>{{ msg }}</span> </button> </template> <script> export default { props: { msg: { default: '下载' } } } </script> 组件使用 <template> <div id="myButton"> <ex-btn :msg="msg1"></ex-btn>
苦咖啡
2019/12/11
7430
CSS 自定义属性进阶使用 (二)
通常,为了实现可复用可定制的模块,我希望将某个实现抽象化。例如,下面这段CSS代码:
前端修罗场
2023/10/07
1800
CSS 自定义属性进阶使用 (二)
android自定义属性
1、引言 对于自定义属性,大家肯定都不陌生,遵循以下几步,就可以实现: 自定义一个CustomView(extends View )类 编写values/attrs.xml,在其中编写styleable和item等标签元素 在布局文件中CustomView使用自定义的属性(注意namespace) 在CustomView的构造方法中通过TypedArray获取 ps:如果你对上述几个步骤不熟悉,建议先熟悉下,再继续~ 那么,我有几个问题: 以上步骤是如何奏效的? styleable 的含义是什么?
xiangzhihong
2018/02/01
2.2K0
自定义属性操作
 element.属性 获取属性值。  element.getAttribute('属性');
梨涡浅笑
2020/10/27
7930
自定义属性操作
自定义属性目的:是为了保存并使用数据。有些数据可以保存到页面中而不用保存到数据库中。
星辰_大海
2020/09/30
8440
Android 自定义 view 之自定义布局属性 xml 属性
上一篇点击查看 https://blog.csdn.net/qq_43377749/article/details/91045764 我们讲到了自定义 view 的基本方式,但是我们也发现,使用这种方式,用户(广大程序员同胞们)只能使用父类控件的属性,但是我们有时需要更多的功能,比如:图片控件需要改变透明度,卡片控件需要设定阴影值等等,那么父类控件的属性显然不够用了,这时我们就要开始实现自定义布局。
圆号本昊
2021/09/24
1K0
Android 自定义 view 之自定义布局属性 xml 属性
使用CSS自定义属性实现骨架屏
其实这篇文章网上已经有翻译版本,但是读起来明显是机翻的,实在是受不了,于是就用自己的理解翻译了一下
coder_koala
2022/11/28
9480
使用CSS自定义属性实现骨架屏
【CSS】CSS自定义属性进阶使用(一)
在传统的CSS中,通常我们需要写重复的属性值,而自定义原则能让我们避免这种情况。做到“一处定义,处处使用”。
前端修罗场
2023/10/07
2450
Angular 自定义属性指令
本文将使用 UltimateAngular/angular-pro-src 中的示例,来一步步介绍自定义属性指令的相关知识。在正式开发前,我们可以先看一下,最终效果 Stackblitz - Custom-Attribute-Directive。
阿宝哥
2019/11/05
2.1K0
自定义属性--和calc
最近在弄练习写demo11,写到有关于 --XXX的自定义属性,calc,平时很少用,比较生面口,于是将它mark下来。
张炳
2019/08/02
7280
自定义属性--和calc
spring 自定义配置属性
完成以上几步后构建项目就会在META-INF下生成文件spring-configuration-metadata.json里面包含配置属性的信息 在IDEA环境中编辑对应配置文件会根据这个文件的信息提供感知
路过君
2020/06/19
5180
springboot引用自定义属性
在application.yml 自定义 在需要引用到的变量声明处使用 @Value("${ficos.market-version}") 样例 @Value("${ficos.market-version}") public String mv="1.0"; 注意,如果变量声明在构造函数外,那么这里我运行的时候发现他晚于构造函数赋值 如果要在构造函数里面使用,这里是我查到的,没有试验过
全栈程序员站长
2021/04/07
4670
Android 自定义删除 View
目标:实现一个点击删除的Item 效果图如下:我知道作为研发肯定会吐槽这个删除的设计,但是我还是要上图,不然我们岂不是不明真相的吃瓜群众… 两个删除的按钮 点击减号出现垃圾桶 点击垃圾桶删除当前的i
非著名程序员
2018/02/02
1.2K0
Android 自定义删除 View
使用 data-* 属性来嵌入自定义数据:
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/112709.html原文链接:https://javaforall.cn
全栈程序员站长
2022/07/08
3330
Android自定义属性TypedArray详解
大家好,我是程序员双木L,后续会发专题类的文章,这是自定义控件的第一篇,之后也会陆续更新相关的文章,欢迎关注。
SoullessCoder
2021/05/13
1.5K0
Android自定义属性TypedArray详解
8.2 自定义 Git - Git 属性
你也可以针对特定的路径配置某些设置项,这样 Git 就只对特定的子目录或子文件集运用它们。 这些基于路径的设置项被称为 Git 属性,可以在你的目录下的 .gitattributes 文件内进行设置(通常是你的项目的根目录)。如果不想让这些属性文件与其它文件一同提交,你也可以在 .git/info/attributes文件中进行设置。
shaonbean
2019/05/26
7530
java 获得xml自定义属性
xmlKit import com.jfinal.weixin.sdk.utils.IOUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import javax.xml.namespa
DencyCheng
2019/02/27
7630
实战spring自定义属性(schema)
spring启动后,dubbo的本地运行时环境就会获取到这些信息,根据这些信息完成注册服务,今天我们实战的内容就是开发一个类似的自定义属性,然后在spring项目中使用这个属性;
程序员欣宸
2020/05/26
6330
JS 操作 HTML 自定义属性
<input type="text" id="txtBox" displayName="123456" />   获取自定义属性值:  document.getElementById("txtBox").getAttribute("displayName");    document.getElementById("txtInput").attributes["displayName"].nodeValue 设置自定义属性值: document.all.txtBox.setAttribute("displa
跟着阿笨一起玩NET
2018/09/18
1.9K0
CODING 敏捷开发:如何自定义属性
CODING 承载了最先进的敏捷研发理论,能够帮助您和您的团队快速入门敏捷研发,并通过标准化的流程和完整的信息统计成为企业实践敏捷研发的好工具。在上一篇视频指南中我们对敏捷项目管理中的工作流配置进行了介绍。本期视频中您可以根据企业的需要对需求/任务/缺陷的属性信息进行自定义。
腾讯云 CODING
2019/09/16
4070
CODING 敏捷开发:如何自定义属性

相似问题

PostSharp:使用OnMethodInvocationAspect时删除自定义属性

11

删除自定义属性

14

删除自定义属性

12

使用java库删除自定义属性

10

如何使用javascript删除自定义属性元素?

11
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文