Warning: Attempt to present UINavigationController on SampleViewController: whose view is not in the window hierarchy - xcode4

i have try all the solution in this forum but still cannot solve. Can anybody help me out with these problem? i want the app to open the inboxData class that contain a tableView when the NSUserDefault read the key #"url" from Urban Airship..
this is from SampleViewController class;
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *action_9 = [defaults objectForKey:#"url"];
if ([[[NSUserDefaults standardUserDefaults]objectForKey:#"url"] isEqualToString:#"aa9"])
{
inboxData *screen=[[inboxData alloc]initWithNibName:#"inboxData" bundle:nil];
screen.strGetTableSelect=#"1";
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:screen];
[self presentModalViewController:navigationController animated:YES];
}
}
it return with these error..
Warning: Attempt to present UINavigationController on SampleViewController whose view is not in the window hierarchy

Your SampleViewController not in the Window hierarchy, You need to set in window.
In your appDelegate, do like this,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[SampleViewController alloc] initWithNibName:#"SampleViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
If you are using storyboad, Use like this in didFinishLaunchingWithOptions method,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone"
                                                             bundle: nil];
 
    SampleViewController *mainViewController = (SampleViewController*)[mainStoryboard
                                                       instantiateViewControllerWithIdentifier: #"SampleViewController"];
 
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mainViewController];
 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window setRootViewController:navigationController];
    [self.window setBackgroundColor:[UIColor whiteColor]];
    [self.window makeKeyAndVisible];
 
    return YES;
}
In your view controller,
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *action_9 = [defaults objectForKey:#"url"];
if ([[[NSUserDefaults standardUserDefaults]objectForKey:#"url"] isEqualToString:#"aa9"])
{
inboxData *screen=[[inboxData alloc]initWithNibName:#"inboxData" bundle:nil];
screen.strGetTableSelect=#"1";
[self presentModalViewController:screen animated:YES];
}
}

Related

UICollectionView in UINavgationController

I want to do a simple example.
I have a rootViewController that is a UINavgationController, and there is a UIViewController in that UINavgationController. Now I want to create UICollectionView in the UIViewController, but I am getting an error.
here is the code:
#import "AppDelegate.h"
#import "XJViewController.h"
#interface AppDelegate ()
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
XJViewController *xjv = [[XJViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:xjv];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
#import <UIKit/UIKit.h>
#interface XJViewController : UIViewController<UICollectionViewDelegate,UICollectionViewDataSource>
#end
#import "XJViewController.h"
#interface XJViewController ()
#end
#implementation XJViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
self.title = #"XJ";
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
NSDictionary *dicColor = #{NSForegroundColorAttributeName : [UIColor whiteColor]};
self.navigationController.navigationBar.titleTextAttributes = dicColor;
UICollectionViewFlowLayout *fLayout = [[UICollectionViewFlowLayout alloc]init];
UICollectionView *cv = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:fLayout];
[cv registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"myeCell"];
cv.dataSource = self;
cv.delegate = self;
[self.view addSubview:cv];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 4;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"myCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
return cell;
}
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view
of kind: UICollectionElementKindCell with identifier myCell - must
register a nib or a class for the identifier or connect a prototype
cell in a storyboard'
Please help me figure out what the problem is.
[cv registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"myeCell"];
The identifier here is different from the one you are using in -cellForItemAtIndexPath

Combining a TabBar and Navigation Controller IOS8

I'm new to iOS and I have troubles to display a navigation controller in my taBbar after Xcode 6 update.
With Xcode 5, it was working perfectly but now on the simulator I get an error message "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported'" and the app crashes.
Here is my code:
**appDelegate.m**
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor =[UIColor whiteColor];
[self.window makeKeyAndVisible];
*// tabBar items*
ItemUnViewController *itemUnViewController = [[ItemUnViewController alloc]
initWithNibName:nil
bundle:NULL];
ItemDeuxViewController *itemDeuxViewController = [[ItemDeuxViewController alloc]
initWithNibName:nil
bundle:NULL];
*//tabBar*
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:#[itemUnViewController,itemDeuxViewController]];
*//setting tabBar as rootView*
self.window.rootViewController = tabBarController;
*// navigation Controllers*
UINavigationController *itemUnNavigationController =
[[UINavigationController alloc]
initWithRootViewController:itemUnViewController];
UINavigationController *itemDeuxNavigationController =
[[UINavigationController alloc]
initWithRootViewController:itemDeuxViewController];
*//Combining tabBar and Navigation Controllers*
[tabBarController setViewControllers:#
[itemUnNavigationController,itemDeuxNavigationController]];
return YES;
}
I even tried this way http://blog.rifkilabs.net/exploring-navigation-controller-and-tab-bar-controller.html but i get the same error message.
Thanks for your help.
tabBar_Controller = [[UITabBarController alloc]init];
[tabBar_Controller setDelegate:self];
ViewController1 *obj_1 = [obj_story instantiateViewControllerWithIdentifier:#"VC1"];
UINavigationController *navi_vc1 = [[UINavigationController alloc]initWithRootViewController:obj_1];
navi_vc1.navigationBarHidden = YES;
ViewController2 *obj_2 = [obj_story instantiateViewControllerWithIdentifier:#"VC2"];
UINavigationController *navi_vc2 = [[UINavigationController alloc]initWithRootViewController:obj_2];
navi_vc2.navigationBarHidden = YES;
[tabBar_Controller setViewControllers:[NSArray arrayWithObjects:navi_vc1,navi_vc2,nil]];
//Hide Tabbar View Controller.//
[self hideTabBar]
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-(void) hideTabBar
{
for(UIView *view in tabBar_Controller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, [UIScreen mainScreen].bounds.size.height, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, [UIScreen mainScreen].bounds.size.height)];
}
}
}
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-(void)create_BottomBarButton
{
UIView *viewBottom = (UIView *)[self.window viewWithTag:Tag_BottomView];
if (viewBottom == nil)
{
#try
{
viewBottom = [[UIView alloc]init];
[viewBottom setFrame:CGRectMake([UIScreen mainScreen].bounds.origin.x, [UIScreen mainScreen].bounds.size.height-(HeightOfBottomBar+KsubviewHeight), [UIScreen mainScreen].bounds.size.width, HeightOfBottomBar)];
[viewBottom setBackgroundColor:RGB(47.0, 45.0, 45.0, 1.0)];
[viewBottom setTag:Tag_BottomView];
NSArray *arrImage = [NSLocalizedString(#"arrBottomBarImgs", nil)componentsSeparatedByString:#","];
float x = 0.0;
for (int i =0; i < kTotalTab; i++) {
[self create_TabBtn:viewBottom frame:CGRectMake(x, 0, 62,62) withTag:i+1 withImage:[arrImage objectAtIndex:i]];
x += 64.5;
}
[self.window addSubview:viewBottom];
}
#catch (NSException *exception)
{
NSLog(#"EXCEPTON %#",[exception description]);
}
}
}
-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-
-(void)select_Tab:(UIButton *)btn
{
#try {
[[tabBar_Controller.viewControllers objectAtIndex:btn.tag-1] popToRootViewControllerAnimated:YES];
[tabBar_Controller setSelectedIndex:btn.tag-1];
}
#catch (NSException *exception) {
NSLog(#"%s",__PRETTY_FUNCTION__);
}
}
-=-=-=-=-=-=-=-=-=-=-=-=-=-
-(void)pushTabBarIntoNavigationBar
{
[navi setNavigationBarHidden:YES];
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[navi pushViewController:tabBar_Controller animated:YES];
}
-=-=-=-=-=-=-=-=-=-=-=
navi = [[UINavigationController alloc]initWithRootViewController:tabBar_Controller];
[navi setNavigationBarHidden:YES];
self.window.rootViewController = navi;

Disabling UINavigation Buttons In Array

I've got two buttons on the right side of my UINavigationController that are working as expected. This is the code I'm using to make this happen:
// Share Button
UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
[shareButton setFrame:CGRectMake(0,0,19,21)];
[shareButton addTarget:self action:#selector(shareButton) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *shareBarButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(shareButton)] autorelease];
shareBarButton.tintColor = [UIColor whiteColor];
// Snapback Button
UIButton *navSnapbackButton = [UIButton buttonWithType:UIButtonTypeCustom];
[navSnapbackButton setFrame:CGRectMake(0,0,26,21)];
[navSnapbackButton setImage:[UIImage imageNamed:#"Snapback.png"] forState:UIControlStateNormal];
[navSnapbackButton addTarget:self action:#selector(snapbackButton) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *navSnapbackBarButton = [[[UIBarButtonItem alloc] initWithCustomView:navSnapbackButton] autorelease];
navSnapbackButton.tintColor = [UIColor whiteColor];
// Right Toolbar Button Setup
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:navSnapbackBarButton, shareBarButton, nil]];
I have a simple request: how do I disable a button in this array? For example say under a specific condition I wanted the Share button disabled, how would I go about doing this? Thanks in advance!
Instead of a local variable for your UIBarButtonItem *shareBarButton, declare it as a property in your #interface section so that you have access to it in other methods:
#property (strong, nonatomic) UIBarButtonItem *shareBarButton;
...
Then assign it in your toolbar setup routine as you have done:
// Share Button
shareBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(shareButton:)];
shareBarButton.tintColor = [UIColor whiteColor];
...
Action for the button looks like this:
-(void)shareButton:(id)sender {
}
...
Then in the shareButton action, or some other method, you can now set the enabled property:
self.shareBarButton.enabled = NO;
if (...) {
self.shareBarButton.enabled = YES;
}
The answer of Ann Taylor is very nice and I will do in that way, but if you don't want to have this UIBarButton like a interface property in your view, you can do this:
UIBarButtonItem *btn1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(performAction:)];
btn1.tag = 0;
UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"_ui_image"] style:UIBarButtonItemStylePlain target:self action:#selector(performAction:)];
btn2.tag = 1;
self.navigationItem.rightBarButtonItems = #[btn1,btn2];
And in the disabled method:
- (void)performAction:(id)sender
{
NSArray *_barButtons = self.navigationItem.rightBarButtonItems;
UIBarButtonItem *_barButton;
for (_barButton in _barButtons) {
if (_barButton.tag == 0) {
_barButton.enabled = NO;
}
}
}
I.

GPUImageView Integration in my project

What I am trying to do is this.
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url2 = [[NSBundle mainBundle] URLForResource:#"NAN" withExtension:#"mov"];
GPUImageMovie *movie2 = [[GPUImageMovie alloc] initWithURL:url2];
movie2.playAtActualSpeed = YES;
filter0 = [[GPUImageSepiaFilter alloc] init];
[movie2 addTarget:filter0];
[filter0 addTarget:view0]; // view0 is a GPUImageView taken in nib
[self recordVideo];
isRecording = YES;
[movie2 startProcessing];
[movieWriter setCompletionBlock:^{
if (isRecording) {
[self stopRecording];
isRecording = NO;
} }];
}
-(void)recordVideo {
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:#"file.mov"];
NSURL *url = [NSURL fileURLWithPath:path];
NSFileManager *fm = [NSFileManager defaultManager];
[fm removeItemAtPath:path error:nil];
movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:url size:CGSizeMake(640, 480)];
[filter0 addTarget:movieWriter];
[movieWriter startRecording];
}
-(void)stopRecording {
[movieWriter finishRecording];
}
But this is not working at all. I always get a black video with this code. There is no crash, I get a black video with the same duration as of original video. Has anyone faced the same issue. Kindly help.
Any help would be great.

ZUUIRevealController - Memory allocations keeps on increasing & slow UIAlertView & UIActionsheet

I am starting with new iOS app in which I have to use Reveal control. So I planned to use great ZUUIRevealController library. I just created UI for 2 - 3 screens . In one of those screen I have to shown UIAlertView & UIActionsheet. But I realised that UIAlertView & UIActionsheet shows very slowly (it adds black overlay first & then shows UIAlertView/UIActionsheet, kind of alert view hangs while showing. Animation is not smooth). I didn't added any logic for coding just created the UI. I didn't use any background thread / dispatch_asyn(). So there is no issue of updating UI from background thread. So I checked the memory leaks & allocations. I didn't see any memory leaks , but memory allocations keeps on increasing while I reveal the menu & push new view controller. It doesn't decrese when I pop the view controller. Here are the screenshots :
1) When app launches
2) After some time , when I push new view controller, pop view controller
I know ZUUIRevealController library creates new instance of view controller every time I select a row from Menu. So I crated the instance variables of view controllers in .h file , but still I have same problem.
Here is my code
// AppDelegate.m
// On SplashViewController , I decide whether to show menu or Login screen
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.splashViewController = [[SplashViewController alloc]initWithNibName:#"SplashViewController" bundle:nil];
self.window.rootViewController = self.splashViewController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[self customizeUIControlsAppearance];
return YES;
}
// SplashViewController.m
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
BOOL isLoggedIn = YES;
if(isLoggedIn){
[self showMenuViewController];
}
else{
[self showStartUpViewController];
}
}
-(void)showStartUpViewController
{
StartUpViewController *startUpViewController = [[StartUpViewController alloc]initWithNibName:#"StartUpViewController" bundle:nil];
UINavigationController *navController=[[UINavigationController alloc] initWithRootViewController:startUpViewController];
[self presentViewController:navController animated:NO completion:^{
}];
}
-(void)showMenuViewController
{
MenuViewController *menuViewController=[[MenuViewController alloc] init];
CategoryViewController *categoryViewController=[[CategoryViewController alloc]initWithNibName:#"CategoryViewController" bundle:nil];
categoryViewController.isViewControllerShownFromMenu = YES;
UINavigationController *navController=[[UINavigationController alloc] initWithRootViewController:categoryViewController];
RevealController *revealController=[[RevealController alloc] initWithFrontViewController:navController rearViewController:menuViewController];
[self presentViewController:revealController animated:NO completion:^{
}];
}
// MenuViewController.m
// SettingsViewController & NewsViewController are decalred in .h file with property & syntheszie
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
RevealController *revealController = [self.parentViewController isKindOfClass:[RevealController class]] ? (RevealController *)self.parentViewController : nil;
// Switch using Section Number
switch (indexPath.section) {
case OTHER_SECTION:
switch (indexPath.row) {
case NEWS:
[self showNewsViewControllerUsingRevealController:revealController];
break;
case SETTINGS:
[self showSettingsViewControllerUsingRevealController:revealController];
break;
default:
break;
}
break;
default:
break;
}
}
#pragma mark - Show News & Settings View Controllers
-(void)showNewsViewControllerUsingRevealController:(RevealController*)revealController
{
if ([revealController.frontViewController isKindOfClass:[UINavigationController class]] && ![((UINavigationController *)revealController.frontViewController).topViewController isKindOfClass:[NewsViewController class]])
{
if(!self.newsViewController)
self.newsViewController = [[NewsViewController alloc] initWithNibName:#"NewsViewController" bundle:nil];
self.newsViewController.isViewControllerShownFromMenu = YES;
UINavigationController *navigationController= [[UINavigationController alloc] initWithRootViewController:self.newsViewController];
[revealController setFrontViewController:navigationController animated:NO];
}
else
{
[revealController revealToggle:self];
}
}
-(void)showSettingsViewControllerUsingRevealController:(RevealController*)revealController
{
if ([revealController.frontViewController isKindOfClass:[UINavigationController class]] && ![((UINavigationController *)revealController.frontViewController).topViewController isKindOfClass:[SettingsViewController class]])
{
if(!self.settingsViewController)
self.settingsViewController= [[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:nil];
self.settingsViewController.isViewControllerShownFromMenu = YES;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.settingsViewController];
[revealController setFrontViewController:navigationController animated:NO];
}
else
{
[revealController revealToggle:self];
}
}
// Code for shwoing UIAlertView & UIActionsheet
-(void)showAlertWithTitle:(NSString*)title andMessage:(NSString*)message
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:title message:message delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
-(void)btnInviteTapped:(id)sender event:(UIEvent*)event
{
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:NSLocalizedString(#"FRIENDS_VC:INVIT_ACTIONSHEET_TITLE", nil) delegate:self cancelButtonTitle:NSLocalizedString(#"FRIENDS_VC:ACTIONSHEET_CANCEL_BUTTON",nil) destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(#"FRIENDS_VC:ACTIONSHEET_SMS_BUTTON",nil),NSLocalizedString(#"FRIENDS_VC:ACTIONSHEET_EMAIL_BUTTON",nil), nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[actionSheet showInView:self.view];
}
By the way I have 11 menu items.
My Login & Regsiter flow is not embedded in MenuViewController. On Login & REgsiter , UIAlertView & UIActionSheet shows smoothly.
Any idea whats going wrong here ? Did I make some mistake in coding ?
Why my UIAlertView & UIActionsheet are not showing smoothly ?
Any kind of help is appreciated. Thanks.

Resources