在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,但由于我在页面上有其他标签,我希望在slComposeViewController setInitialText
之外分配NSString值。有什么建议吗?
谢谢! 罗布
答案 0 :(得分: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;