0

ビューコントローラーとクラスがあります(ストーリーボードを使用しています)。UILabelクラスから (View Controller 内の) a のテキストを変更するにはどうすればよい ですか? 私はこれを試しましたが、役に立ちませんでした:

ViewController *mainView = [[ViewController alloc] init];
[mainView view];

[mainView.progressBar setProgress:integer animated:YES];

NSLog(@"Updated Progress Bar");

NSString *progressLabelText = [NSString stringWithFormat:@"%@ out of %i followers", userString, [self.followers count]];

[mainView.progressLabel setText:progressLabelText];

NSLog(@"Updated Progress Label Text: %@", progressLabelText);

このコードを使用しても、テキストは変更されません。代わりに何をすべきですか?

編集: ViewController の .h ファイルのプログレスバーとラベルは次のようになります。

@property (nonatomic, strong) IBOutlet UIProgressView *progressBar;
@property (nonatomic, strong) IBOutlet UILabel *progressLabel;

これらは Interface Builder で完全にリンクされています。

4

4 に答える 4

3

ラベル テキストの更新に Notification を使用してみてください。

あなたのviewControllerにこれを書いてください:

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLabel) name:@"lblUpdate" object:nil];


  and selector is :
   -(void)updateLabel
{
    self.lblObject.text = @"Your updated text"
}

クラスで、Post 通知を使用してこれを呼び出します。

 [[NSNotificationCenter defaultCenter] postNotificationName:@"lblUpdate" object:nil];

同じ通知名「lblUpdate」を使用することを忘れないでください

于 2013-01-07T15:00:34.947 に答える
3

またはの間で使用delegateします。messagingclassesviewcontrollers

プロトコルとデリゲートの基本リンクを参照してください。

于 2013-01-07T13:16:31.993 に答える
0

ただしdelegate、コールバックを行うには a を使用します。あなたのspamchecker.h追加で:

@protocol SpamCheckerDelegate <NSObject>
-(void)updateText:(NSString*)newText;
@end

@property (nonatomic, weak) id <SpamCheckerDelegate> delegate;

spamchecker.m必要な場所に追加します:

[self.delegate updateText:@"newText"];

次に、mainView.h追加で:

@interface MainView : UIViewController <SpamCheckerDelegate>

あなたのmainview.m追加で:

spamchecker.delegate = self;

そして、次のようなメソッドを実装します。

-(void)updateText:(NSString*)newText
{
    [self.progressLabel setText:progressLabelText];
}
于 2013-01-07T13:12:36.513 に答える
0

文字列をプロパティ合成し、その文字列をクラスviewWillAppear:のメソッドに設定するだけですViewController.m

NSString次のようにファイル内の変数を取得するだけViewController.hです..

@property (nonatomic, retain) NSString *progressText;

そして、 synthesizeこれは以下のViewController.mようなファイルにあります..

@synthesize progressText;

その後、viewDidLoad:メソッドで次のように開始時にテキストを設定するだけprogressLableTextです...

- (void)viewDidLoad
{

     progressText = @"Your Text";
     [progressLabel setText:progressText];

}

また、viewWillAppear:上記のようなテキストを設定します...

- (void)viewWillAppear:(BOOL)animated
{
    [progressLabel setText:progressText];
}

そして、あなたのコードでは、以下のようなものを変更するだけです..

ViewController *mainView = [[ViewController alloc] init];
[mainView view];

[mainView.progressBar setProgress:integer animated:YES];

NSLog(@"Updated Progress Bar");

NSString *progressLabelText = [NSString stringWithFormat:@"%@ out of %i followers", userString, [self.followers count]];

mainView.progressText = progressLabelText;
[mainView.progressText retain];


NSLog(@"Updated Progress Label Text: %@", progressLabelText);

これがお役に立てば幸いです...

于 2013-01-07T13:21:36.133 に答える