私はプログラミングとObjective-Cには非常に新しいので、何が間違っているかを試しています私のコード。私はブロックについて少しは読んだことがありますが、私がこれまでに読んだことが私のコードにどのように関係しているか分かりません。iOS 5 TwitterフレームワークとcompletionHandlerブロック - 「このブロック内で 'self'を強く捕捉すると保持サイクルにつながる」
私のコードでは、iOS 5 Twitter Frameworkが使用されています。 Appleが提供するほとんどのサンプルコードを使用しているため、最初に補完ハンドラのブロックを使用しているという手がかりはありませんでした。
今、私は「1.ブロックが強く被写体によって保持されたオブジェクトによって保持されます」と、このブロックで強く「キャプチャ 『自己』につながる可能性が高いと言ってXcodeの4からこれらの2つのメッセージを取得します保持サイクル "。
基本的に、私がやったことは自分の完了ハンドラ(TWTweetComposeViewControllerResultCancelled & TWTweetComposeViewControllerResultDoneとswitch文)で使用されるコードのアップルを削除することであり、私の[imagePickerController sourceType]
とif文を使用していました。
したがって、画像がツイートに追加された後にsendTweet
が呼び出されます。
私は、なぜこれが起こっているのか、どうすれば解決できるのか誰かが私に説明できることを願っています。また、補完ハンドラコードをブロックではなくメソッドに入れることはできますか?
- (void)sendTweet:(UIImage *)image
{
//adds photo to tweet
[tweetViewController addImage:image];
// Create the completion handler block.
//Xcode: "1. Block will be retained by an object strongly retained by the captured object"
[tweetViewController setCompletionHandler:
^(TWTweetComposeViewControllerResult result) {
NSString *alertTitle,
*alertMessage,
*otherAlertButtonTitle,
*alertCancelButtonTitle;
if (result == TWTweetComposeViewControllerResultCancelled)
{
//Xcode: "Capturing 'self' strongly in this block is likely to lead to a retain cycle"
if ([imagePickerController sourceType])
{
alertTitle = NSLocalizedString(@"TCA_TITLE", nil);
alertMessage = NSLocalizedString(@"TCA_MESSAGE", nil);
alertCancelButtonTitle = NSLocalizedString(@"NO", nil);
otherAlertButtonTitle = NSLocalizedString(@"YES", nil);
//user taps YES
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:alertTitle
message:alertMessage
delegate:self // Note: self
cancelButtonTitle:alertCancelButtonTitle
otherButtonTitles:otherAlertButtonTitle,nil];
alert.tag = 1;
[alert show];
}
}
ありがとうございました!私は総初心者なので、実際にこれを実装する方法を考え出すのに問題がありました。最後に私は__weak UIImagePickerController * weakSelf = imagePickerControllerを使用しました。 if文をif([weakSelf sourceType])に変更します。 Xcode 4は私にもうエラーを表示していないので、正しいことをしたと思います。 (?) – iMaddin
+1 @Dennisは答えに感謝します。 「__block」が使用されている場合も説明してください。例iには、 のような構文があります。 __block HomeViewController * weakSelf = self; – HDdeveloper