xcode で変数をボタンに渡す方法があるかどうかを確認しています。NSString に保存している API から取得している Web リンクがあります。クリックしたときにそれに応じてリンクをたどることができるように、それをボタンに渡す方法があるかどうか疑問に思っています。
2 に答える
2
その URL をいくつかの ivar またはプロパティに保存します。通常どおり、ボタンにアクションを割り当てます。アクションは、View Controller のメソッドにほかなりません。そのメソッド内で、そのリンクを実行します。
これは、リンクを「たどる」方法です。
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.klecker.de"]];
あなたの場合、この例で使用している定数の代わりに NSString 変数を使用します。
于 2013-01-21T21:05:42.657 に答える
0
UIBUttonをサブクラス化して、好きな/必要なプロパティ/変数を追加することができます。あなたの説明から、NSStringの代わりにNSURLを使用します...
.hファイルに新しいUIButtonサブクラスを作成します。
#import <UIKit/UIKit.h>
@interface MyButton : UIButton
{
NSURL* buttonUrl;
}
@property (nonatomic, retain) NSURL* buttonUrl;
@end
.mファイルでは単純に次のようになります。
#import "MyButton.h"
@implementation MyButton
@synthesize buttonUrl;
@end
次に、ViewControllerで:(「MyButton.hを#importすることを忘れないでください)
MyButton *theButton = [[MyButton alloc] initWithFrame:someframe];
[theButton addTarget:self action:@selector(buttonPushed:) forControlEvents:UIControlEventTouchUpInside];
theButton.buttonUrl = [NSURL URLWithString:apiString];
[self.view addSubView:theButton];
[theButton release];
...そして、ボタンが押されたときに、次のようにURLを再度取得できます。
-(void)buttonPushed:(id)sender{
NSURL* theUrl = [(MyButton*)sender getButtonUrl];
}
于 2013-01-21T21:23:56.977 に答える