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
ます。助言がありますか?
ありがとう!ロブ