2014-01-29 67 views
1

我有一個帶有xib文件的viewController B類,並且我將這個viewController添加到父視圖中。iOS從子視圖控制器隱藏statusBar

[Acontroller addChildViewController:self]; // self is B 
[Acontroller.view addSubview:self.view]; 

我想要的是,當顯示B viewController時,隱藏應用程序的狀態欄。

如果我在A控制器使用方法:

-(BOOL)preferStatusBarHidden { 

return YES; 

} 

狀態欄被隱藏,但我想這樣做,只是從B控制器,但它不工作。

我該怎麼辦?

謝謝。

View Controller B : 

// 
// AppehourInterstitielAds.m 
// AppTestSdk 
// 
// Created by Administrateur on 22/01/2014. 
// Copyright (c) 2014 R. All rights reserved. 
// 

#import "AppehourInterstitielAds.h" 
#import "AppehourSdk.h" 

@interface AppehourInterstitielAds() 

@end 

@implementation AppehourInterstitielAds 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

-(id)initWithViewController:(UIViewController*)controller;{ 



    if (![AppehourSdk isNetworkConnected]){ 

     return nil; 
    } 


    self = [super init]; 
    vController = controller ; 


    //------ Chargement des différents layouts selon taille écran 

    // iPAD 
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){ 

     self.view = [[NSBundle mainBundle]loadNibNamed:@"interstitielAdsIPad" owner:self options:nil][0]; 
    } 
    //iPhone 
    else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){ 
     CGSize size = [[UIScreen mainScreen]bounds].size; 

     //iphone 3.5" 
     if (size.height == 480){ 

      self.view = [[NSBundle mainBundle]loadNibNamed:@"interstitielAdsIPhone3.5" owner:self options:nil][0]; 

     } 
     //iphone 4" 
     else { 

      self.view = [[NSBundle mainBundle]loadNibNamed:@"interstitielAdsIPhone4" owner:self options:nil][0]; 

     } 
    } 

    //------------- Construction requête 


    if ([[AppehourSdk getDeviceId]isEqualToString:NSLocalizedStringFromTable(@"device_debug_id", @"appehourValues", nil)]){ 

     NSLog(@"is debug device"); 
     isDebugDevice = @"true"; 
    } 
    else { 

     isDebugDevice = @"false" ; 
    } 

    frame = self.webViewInterst.frame ; 

    width = [NSString stringWithFormat:@"%d", (int)frame.size.width]; 
    height = [NSString stringWithFormat:@"%d", (int)frame.size.height]; 

    NSLog(@"ads interstitiel width: %@", width) ; 
    NSLog(@"ads interstitiel height: %@", height) ; 


    NSError *error; 

    NSMutableDictionary *dictionnaryDatas = [[NSMutableDictionary alloc]init]; 
    NSMutableDictionary *dictionnaryDatasAds = [[NSMutableDictionary alloc]init]; 


    [dictionnaryDatasAds setObject:width forKey:@"width"]; 
    [dictionnaryDatasAds setObject:height forKey:@"height"]; 
    [dictionnaryDatasAds setObject:@"interstitiel" forKey:@"type"]; 
    [dictionnaryDatasAds setObject:[AppehourSdk getDeviceId] forKey:@"device_id"]; 
    [dictionnaryDatasAds setObject:[AppehourSdk getAppId] forKey:@"app_id"]; 
    [dictionnaryDatasAds setObject:isDebugDevice forKey:@"is_debug_device"]; 


    // conversion en json 
    NSData *datas = [NSJSONSerialization dataWithJSONObject:dictionnaryDatasAds options:0 error: &error]; 


    // url datas 
    [dictionnaryDatas setObject:NSLocalizedStringFromTable(@"campaign_id", @"appehourValues", nil) forKey:@"cid"]; 
    [dictionnaryDatas setObject:@"ads" forKey:@"cat"]; 
    //ajout du json dans data 
    [dictionnaryDatas setObject:datas forKey:@"data"]; 

    NSData *datasToSend = [NSKeyedArchiver archivedDataWithRootObject:dictionnaryDatas]; 

    NSURL *nsUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@", @"http://",TAG_DOMAIN,@"/scripts/cpi.php" ]]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nsUrl]; 


    [request setHTTPMethod:@"POST"]; 
    // ajout des datas a la requete 
    [request setHTTPBody:datasToSend]; 

    // Create url connection and fire request 
    NSURLConnection *connectionGetAds = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 


    //--------------- End build request---------------------- 

    // [vController addChildViewController:self]; // ajout du childViewController au controller principal 
    [vController addChildViewController:self]; 

    self.webViewInterst.scrollView.scrollEnabled = NO; // désactiver scroll dans webView 

    self.webViewInterst.delegate = self ; // call the webView methods 


    return nil; 

} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [[UIApplication sharedApplication]setStatusBarHidden:YES]; 



} 


- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)buttonClose:(id)sender { 


    [self.view removeFromSuperview]; 
    [self removeFromParentViewController]; 
    [timer invalidate]; 


} 

-(void)decrement { 

    if (seconds == 0){ 

     [self.view removeFromSuperview]; 
     [self removeFromParentViewController]; 
     [timer invalidate]; 

    }else { 

     seconds--; 
     _secondsRemain.text = [NSString stringWithFormat:@"%d%@", seconds, @" seconds remaining"]; 
     timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(decrement) userInfo:nil repeats:NO]; 

    } 

} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response { 

    NSLog(@"response code interstitiel : %d", [response statusCode]); 

} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 

    //[responseDatas appendData:data] ; 

} 

-(void)connectionDidFinishLoading:(NSURLConnection*)connection { 

    NSError *error; 

    //dictAds = [NSJSONSerialization JSONObjectWithData:responseDatas options:NSJSONReadingMutableLeaves error:&error]; 

    //----- TEMPORAIRE 

    NSMutableDictionary *tempDict = [[NSMutableDictionary alloc]init]; 

    [tempDict setObject:@"1245" forKey:@"id"]; 
    [tempDict setObject:@"https://associate.w3i.com/Images/integration/ios-NR-I-portrait.png" forKey:@"url"]; 
    [tempDict setObject:@"interstitiel" forKey:@"type"]; 
    [tempDict setObject:@"tok" forKey:@"token"]; 
    [tempDict setObject:@"clic" forKey:@"rem"]; 
    [tempDict setObject:@"10" forKey:@"valid_delay"]; 


    //---------- 

    NSURL *url = [NSURL URLWithString:[tempDict objectForKey:@"url"]]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 

    [self.webViewInterst loadRequest:requestObj]; 

    seconds = [[tempDict objectForKey:@"valid_delay"] intValue] ; // seconds before close ads 

    [NSThread sleepForTimeInterval:3]; // wait before display the view 

    [vController.view addSubview:self.view]; 

} 

// webVew finis chargement 
-(void)webViewDidFinishLoad:(UIWebView *)webView { 



    [vController.view addSubview:self.view]; // ajoute la vue 
    [self decrement]; 

    [self prefersStatusBarHidden]; 
    [vController prefersStatusBarHidden]; 

} 

@end 

查看答:

// 
// ViewController.m 
// AppTestSdk 
// 
// Created by Administrateur on 09/01/2014. 
// Copyright (c) 2014 R. All rights reserved. 
// 

#import "ViewController.h" 
#import "AppehourInterstitielAds.h" 

@interface ViewController(){ 


    AppehourSdk* appehour; 
    AppehourInterstitielAds *ads ; 


} 

@end 

@implementation ViewController 



-(void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [_appehourAds initAds:[AppehourSdk getDeviceId]]; 
    [[UIApplication sharedApplication]setStatusBarHidden:NO]; 

} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

} 

- (void)viewDidAppear:(BOOL)animated 
{ 
     [super viewDidAppear:animated]; 
    } 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
[super viewDidDisappear:animated]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
// Return YES for supported orientations 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
    } 

- (IBAction)button:(id)sender { 

    /* 
    UILocalNotification *notifcation = [[UILocalNotification alloc]init]; 
    notifcation.alertBody = @"Contenu"; 
    notifcation.fireDate = [NSDate dateWithTimeIntervalSinceNow:5]; 
*/ 

    ads = [[AppehourInterstitielAds alloc]initWithViewController:self]; 


    NSString *lemessage = [[NSString alloc] initWithFormat:@"Button clicked !"]; 
    _labelText.text = lemessage; 
    NSLog(@"Button clicked"); 


    NSMutableDictionary *dictionnary = [[NSMutableDictionary alloc]init]; 
    [dictionnary setObject:@"value" forKey:@"key"]; 

    appehour = [[AppehourSdk alloc]init]; 

    [appehour trackEvent:@"Track" :dictionnary :@"8656"]; 


} 


@end 
+0

爲什麼使用addChildViewController來代替推送或呈現。 HTTP://計算器。com/a/8084188/1405008請參閱此更多信息 – CoolMonster

+0

我試過這個,但是崩潰了我的應用程序 – Flofloaud1034

回答

0

寫內部DidLoad()的ViewController A的方法(要顯示狀態欄)

[[UIApplication sharedApplication] setStatusBarHidden:NO]; 

在ViewController B的DidLoad()方法內(不想顯示狀態欄)寫下面的行

[[UIApplication sharedApplication] setStatusBarHidden:YES]; 

添加以下代碼appDelegate.m文件

- (BOOL)prefersStatusBarHidden { 
     return YES; 
} 
+0

這不起作用 – Flofloaud1034

+0

對不起,Flofloaud1034。它是不是隱藏任何觀點?請嘗試在viewWillAppear()方法中編寫此代碼。我已經檢查過了,它正在爲我工​​作。 – Funny

+0

我已經這樣做了,但是當ViewCOntrollerB的內容出現時,法律欄顯示 – Flofloaud1034

0

試試這個,

保持BOOL檢查乙的viewController顯示與否。而在preferStatusBarHidden

- (BOOL)prefersStatusBarHidden { 
    if (bDisplayed) { 
     return YES; 
    } 
    return NO; 
} 

並添加以下B線的viewController

bDisplayed = YES; 
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) 
{ 
    // if iOS 7 
    [self prefersStatusBarHidden]; 
    [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)]; 
} else { 
    // if iOS 6 
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide]; 
} 
+0

我不能那樣做,因爲B是其他開發人員使用的庫,所以我不能告訴他們更改plist – Flofloaud1034

+0

@ Flofloaud1034看到更新後的答案 – Akhilrajtr

0

如果我把這個父視圖控制器,狀態欄被隱藏:

-(BOOL)preferStatusBarHidden { 

return YES ; 

} 

,但也有必要做這個在子視圖控制器和代碼不在子視圖中調用

0

您的視圖控制器作爲一個自定義容器視圖控制器,封裝底層的競爭w控制器。

A是B視圖控制器的自定義容器。由於我們正在添加孩子,我們會告訴容器從哪裏知道是否顯示或隱藏狀態欄。

因此,對於自定義容器,我們需要覆蓋方法childViewControllerForStatusBarHidden。此方法會調用容器,以調用視圖控制器,它調用prefersStatusBarHidden以確定狀態欄可見性的狀態。

相關問題