1

ではSettings.bundle、識別子 のテキスト入力がありますurl_preference

ViewController.hViewController.m、およびストーリーボードを使用してUIWebView、設定から URL を表示するように設定しました。

- (void) updateBrowser {   
    NSString *fullURL = [[NSUserDefaults standardUserDefaults] stringForKey:@"url_preference"];
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [_EmbeddedBrowser loadRequest:requestObj];    
}

これは機能します。

ただし、[設定] の URL が変更された場合、UIWebViewは新しい URL を反映するように更新されません。

アプリをバックグラウンドで実行できないようにすることで、更新された URL が反映されないという問題が解決されます。ただし、新しい問題が発生します。[設定] の URL が変更されていない場合、セッションは保持されません。が変更されたUIWebView場合にのみ更新する必要があります。url_preference

を使用applicationWillEnterForegroundAppDelegate.mて強制的UIWebViewにリロードしようとしましたが、問題が発生しています。

ViewController では、次を実行できます。

- (void)viewDidLoad {
     [self updateBrowser];
}

しかし、App Delegate で同じことを実行しようとしても更新されません。

- (void)applicationWillEnterForeground:(UIApplication *)application
{

    ViewController *vc = [[ViewController alloc]init];
    [vc updateBrowser];
}

(私は、 、およびにも含め- (void) updateBrowser;ましたViewController.h#import "ViewController.h"AppDelegate.m

ありがとうございました。

4

1 に答える 1

2
- (void)viewDidLoad
{
    [self updateBrowser];
    [super viewDidLoad];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self
               selector:@selector(defaultsChanged:)
                   name:NSUserDefaultsDidChangeNotification
                 object:nil];
}

- (void)defaultsChanged:(NSNotification *)notification {
    [self updateBrowser];
}

- (void) updateBrowser {

    NSString *fullURL = [[NSUserDefaults standardUserDefaults] stringForKey:@"url_preference"];
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [_EmbeddedBrowser loadRequest:requestObj];

}

幸いなことに、このような状況では AppDelegate を使用する必要はありません。実際には、デフォルト設定が変更されたときにリッスンする通知があります。ViewController をオブザーバーとして設定し、NSUserDefaultsDidChangeNotification が送信されるたびに関数を実行する必要があります。この通知は、アプリのデフォルト設定が設定で変更されるたびに自動的に発生します。この方法では、設定が変更された場合にのみ、アプリがフォアグラウンドになるたびに更新する必要はありません。

于 2012-10-08T02:06:08.383 に答える