【发布时间】: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。