这可能有助于回答您的问题:我刚刚想通了:
我想我用过 Matt Gemmell 和他的源代码,虽然我可能用过 Ben Gottlieb,但我不太记得了。不过很确定是马特。
然而,我还创建了一个隐藏的子视图,其中包含实际的推文文本字段、推文按钮和返回按钮,以便用户可以再次隐藏该视图。
总而言之,我有一个显示最初隐藏的子视图的按钮,当它这样做时,它会验证用户。然后,此子视图具有用于发布推文的推文按钮。这是我发现对我有用的代码。现在我只做了最少的测试,但我希望你们可以自己动手,从这里开始。
-(IBAction)shareTwit:(id)sender { //This shows the tweet view and authorises the user and engine
tweetView.hidden = NO;
if (!_engine) {
_engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate:self];
_engine.consumerKey = kOAuthConsumerKey;
_engine.consumerSecret = kOAuthConsumerSecret;
}
if (![_engine isAuthorized]) {
UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine:_engine delegate:self];
if (controller) {
[self presentModalViewController: controller animated: YES];
}
else {
[_engine sendUpdate: [NSString stringWithFormat: @"Already Updated. %@", [NSDate date]]];
}
}
}
-(IBAction)updateTwitter:(id)sender { //This button sends the tweet to Twitter.
if ([_engine isAuthorized]) {
[_engine sendUpdate:tweetTextField.text];
[tweetTextField resignFirstResponder];
confirmation.text = @"Tweet sent successfully."; //This is an optional UILabel, if you would like the user to know if the tweet has been sent
// Currently, I haven't created a method where the user is told the sending failed.
}
else {
UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine:_engine delegate:self];
if (controller) {
[self presentModalViewController: controller animated: YES];
}
else {
[_engine sendUpdate: [NSString stringWithFormat: @"Already Updated. %@", [NSDate date]]];
}
}
}
请注意,我没有使用 a viewDidLoad
,主要是因为我不同意它。我希望用户首先点击shareTwit
按钮来激活身份验证。然后在启动的视图中,他们可以编写他们的推文。我希望这可以帮助你!