重複の可能性:
Objective-Cシングルトンはどのように見えるべきですか?
シングルトンの使い方を理解しようとしています。私は彼らに注意するために赤を持っていますが、彼らが彼らの積極的な用途を持つことができることも。
私のシナリオ:
現在、テストプロジェクトを設定しています。1つのViewControllerには、アクションを実行するために必要なボタンがあります。
FirstViewControllerにはUIWebViewがあります。
StoryboardとContainerViewを使用しているので、両方のViewControllerを同時に表示できます。
最初のViewControllerでは、.mファイルに次のコードがあります。
static FirstViewController *sharedController = nil;
+ (FirstViewController *)sharedController {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
//BBCSermonTabBarViewController *myVC = (BBCSermonTabBarViewController *)[storyboard instantiateViewControllerWithIdentifier:@"BBCNews"];
if(sharedController == nil)
sharedController = (FirstViewController *)[storyboard instantiateViewControllerWithIdentifier:@"firstViewController"];
return sharedController;
}
また、アルファを次のように変更するメソッドもあります。
-(void)hideWebView
{
_webView.alpha = 0.3;
}
これで、SecondViewコントローラーに次のコードがあります。
-(IBAction)hideWebViewFromAnotherViewController
{
[[FirstViewController sharedController] hideWebView];
}
そのアクションボタンは、他のViewControllerのwebViewのアルファを変更する必要がありますか?
そうでなければ、私は何を間違っているのですか?
前もって感謝します:-)