1

いくつかの設定属性を持つアカウントがあります。設定として、のUISlider値との値がありますUISwitchNSUserDefaultsアプリケーションを実行すると、正常に動作します。viewDidLoadメソッドが動作するため、から最後の値を表示できます。ちなみに私のアプリケーションにはタブバーコントローラーがあります。したがって、タブを切り替えると、スイッチとスライダーの値を取得してviewWillAppearメソッドで更新できるため、これも正常に機能します。しかし、私の設定スイッチでは、ユーザーリストが含まれているビューを表示するので、ユーザーは任意のアカウントを選択できます。表示されたビューから戻ると、スイッチとスライダーの値を更新できません。それらの値を更新するためのトリガーメソッドが必要です。それを行う方法はありますか?

4

2 に答える 2

0

私はこれを少し前に書き、基本的にこれらすべての質問に貼り付けます。ところで、誰かがこれを理解しやすくする/より良くする提案があれば、私はそれらを聞いてみたいです。

デリゲートを使用して、クラス間で情報を渡すことができます。これは、メソッドを使用するときに情報を渡したい場合に特に便利です。

Delegates

//In parent .m file:
//assign the delegate
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"segueName"])
    {
        childController *foo = segue.destinationViewController;
        foo.delegate = self;
    }

}

//implement protocol method(s):
- (void) methodName:(dataType*) dataName
{
    //An example of what you could do if your data was an NSDate
    buttonLabel.titleLabel.text = [[date description] substringToIndex:10];
}

//In parent .h file:
//import child header
#import "ChildName.h"

//indicate conformity with protocol
@interface ParentName : UIViewController <ChildNameDelegate>

//In child .h file
//declare protocol
@protocol ChildNameDelegate
- (void) methodName:(dataType*) dataName;
@end

//declare delegate
@property (weak, nonatomic) id<ChildNameDelegate> delegate;


//In child .m file
//synthesize delegate
@synthesize delegate; 

//use method
- (IBAction)actionName:(id)sender 
{
    [delegate methodName:assignedData];
}
于 2012-07-20T13:18:23.933 に答える
0

はい、これはそれを行うための3つのリンゴの方法です:

1) Delegation
2) NSNotificationCenter
3) KVO

ただし、特定のケースではモーダルビューの場合、委任パターンはAppleが推奨するものであり、私も推奨するものです。

ただし、他の2つのオプションよりも少し多くのコーディングが必要です。

まず、モーダルで提示しているビューでプロトコルを宣言する必要があります。

in your .h

@protocol MyProtocol;

@interface MyClass : NSObject

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

@end

@protocol MyProtocol <NSObject>

-(void)updateValues:(NSArray *)values;

@end

次に、元のビューコントローラからモーダルビューをモーダルに表示する前に、デルゲートを次のように設定するだけです。

myModallyPresentedController.delegate = self;

次に、提示するViewControllerにプロトコルを採用させます

in your .h

MyPresentingViewController : UIViewController <MyProtocol>

in .m

//Implement method:

-(void)updateValues:(NSArray *)values {
   //Update Values.
}

最後に、ユーザーが「完了」を押したとき

あなたは呼び出すことができます

[delegate updatedValues:myValues];

それに応じて更新します。

お役に立てば幸いです。

于 2012-07-20T13:19:23.213 に答える