Skip to content

Instantly share code, notes, and snippets.

@setoh2000
Created October 15, 2012 20:25
Show Gist options
  • Save setoh2000/3895173 to your computer and use it in GitHub Desktop.
Save setoh2000/3895173 to your computer and use it in GitHub Desktop.
Twitter.frameworkやSocial.frameworkのツイート画面でカーソルの初期位置を先頭にする
#import <Twitter/Twitter.h>
#import <Social/Social.h>
// TWTweetComposeViewControllerの場合 (for iOS5)
- (IBAction)tweet:(id)sender
{
if ([TWTweetComposeViewController canSendTweet]) {
// ツイート用の画面をを表示する
TWTweetComposeViewController* composeViewController = [[TWTweetComposeViewController alloc] init];
[composeViewController setInitialText:@"Hello World!"];
[self presentViewController:composeViewController animated:YES completion:^{
[self findTextViewAndMoveToTop:composeViewController.view];
}];
}
}
// SLComposeViewControllerの場合 (for iOS6)
- (IBAction)share:(id)sender
{
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
NSString *serviceType = SLServiceTypeTwitter;
SLComposeViewController *composeViewController = [SLComposeViewController composeViewControllerForServiceType:serviceType];
[composeViewController setInitialText:@"Hello World!"];
[composeViewController setCompletionHandler:^(SLComposeViewControllerResult result) {
if (result == SLComposeViewControllerResultDone) {
NSLog(@"Done.");
}
}];
[self presentViewController:composeViewController animated:YES completion:^{
[self findTextViewAndMoveToTop:composeViewController.view];
}];
}
}
// TextViewが見つかればカーソル位置を先頭にする
- (void)findTextViewAndMoveToTop:(UIView *)view
{
if ([view isKindOfClass:[UITextView class]]) {
UITextView *textView = (UITextView *)view;
textView.selectedRange = NSMakeRange(0, 0);
return;
}
for (UIView *subview in [view subviews]) {
[self findTextViewAndMoveToTop:subview];
}
}
@setoh2000
Copy link
Author

ARC環境の方はselfを__weakな変数に代入してから呼び出さないと循環参照になる可能性があるのでご注意下さい。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment