【问题标题】:Xcode 4.6 - Problems posting text assigned to a UILabel to FacebookXcode 4.6 - 将分配给 UILabel 的文本发布到 Facebook 时出现问题
【发布时间】:2013-03-21 12:48:34
【问题描述】:

在 Xcode 4.6 中,我在一个 .xib 页面上有一列标签,每个标签都由文本组成,在其右侧是一组播放按钮,用于播放每个音频,例如在 .m 代码中:

-(IBAction)pushButton {
    NSString *mytextfield = mylabelname.text; 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"mysound" ofType:@"mp3"];
    if(theAudio)[theAudio release];
    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theAudio.delegate = self;
    theAudio.volume = 1.0;
    [theAudio play];
}

播放声音时,我将标签“mylabelname”的关联文本分配给字段 mytextfield。 .h 文件具有 IBOutlet UILabel *mylabelname; UIViewController 中的声明,.m 具有 -(void)mylabelname 并且 Outlet UILabel 连接到 xib 中的标签。

在 .m 文件中,我将 Facebook 发布代码如下:

-(IBAction)ShareFB1 {
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
        slComposeViewController = [[SLComposeViewController alloc] init];
        slComposeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        [slComposeViewController setInitialText:[NSString stringWithFormat:@"Posting to Facebook: %@", mytextfield]];    
        [slComposeViewController addURL:[NSURL URLWithString:@"http://stackoverflow.com"]];
        [self presentViewController:slComposeViewController animated:YES completion:NULL];
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Facebook Account" message:@"There are no Facebook accounts confiured, configure or create accounts in Settings." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}

在 .m 文件中,我收到关于 NSString 的警告:未使用的变量 mylabelname。 slComposeViewController setInitialText 方法也出现错误:使用未声明的标识符“mylabelname”。

我得到这个工作的唯一方法是将NSString *mytextfield = mylabelname.text;添加到-(IBAction)ShareFB1,标签值可以发布到Facebook,但由于页面上有其他标签,我想要NSString在slComposeViewController setInitialText 之外分配的值。有什么建议吗?

谢谢! 抢

【问题讨论】:

  • Rob:我看到了你所有的问题,所有的问题都是链接的,并且有一个小错误。我的建议是花一周时间阅读完整的教程,然后开始开发。当你这样做时,你会节省并感觉良好。您需要在 stackoverflow 上投入 30 分钟,而不是担心简单的错误和警告。
  • 感谢 Anoop,我实际上已经在 Xcode 中开发了一年多,并且在 App Store 上有多个应用程序,但这只是 Objective C 编码的一部分,我还没有必要做。这可能是一项简单的任务,但我还没有遇到向我展示如何执行此操作的教程。我会继续寻找。谢谢,罗伯
  • Rob,对你来说不是什么大问题。无论如何,这个问题的问题是什么?
  • 问题是要发布文本字符串才能工作,我必须声明 NSString *mytextfield = mylabelname.text;在 -(IBAction)ShareFB1 中。我想在 -(IBAction)ShareFB1 之外设置 NSString,例如在同一页面上按下另一个按钮时。这样,发布到 Facebook 的文本字符串可以是动态的,基于相关标签旁边的某些用户操作(例如点击播放按钮)。如果我将 NSString 设置在 -(IBAction)ShareFB1 之外,我会收到 Use of undeclared identifier。

标签: facebook label uilabel


【解决方案1】:

你需要在.h文件中创建一个属性

@property(strong)NSMutableString *stringToPost;//if you wish to append, if replace then use NSString

在你的 .m 文件中,在 viewDidLoad 方法中

self.stringToPost=[NSMutableString new];

并且在所有 IBActions 方法中,您可以附加文本(如果您愿意)或替换之前保存的字符串:

//for appending
[self.stringToPost appendFormat:@"%@",yourTextField.text];

//for replacing with new one
self.stringToPost=yourTextField.text;

【讨论】:

  • 这真的很有帮助!我已经完成了 99% 的路程。您的建议有效,并且可以将字符串发布到 Facebook。我确实在.m中看到了这一点,对于我用新的替换的IBAction:stringToPost=yourTextField.text;我有一个错误:使用未声明的标识符'stringToPost;你的意思是'_stringToPost'吗?我做了修复并使用了_stringToPost,并且还在setInitialText中使用了_stringToPost,然后它工作了,但即使有有效的解决方案我也会收到警告:不兼容的指针类型分配给NSString *的'NSMutableString *' - 我必须查找如何修复不兼容的类型
  • 也许我需要分配和初始化 NSMutableString
  • 是的,我编辑了我的答案。如果您将 NSString 分配给 NSMutableString,那么这就是警告。如果您不必附加,则只需在 .h 和 .m 中转换为 NSString
  • 我尝试添加 [[NSMutableString alloc] initWithString:mylabelname.text];并且还使用了 self.stringToPost=yourTextField.text;但我仍然得到不兼容的指针类型。我现在得先睡一会儿,明天我会更努力地解决这个问题
  • 只是一个快速更新 - Anoop 很棒,他真的让我通过了这个。多亏了他的指导,我觉得我已经彻底破解了这个问题,并且我在我的应用程序中的所有页面上摇摆代码。谢谢阿努普!
猜你喜欢
  • 2018-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多