Custom UITabBarController SIGABRT crash in ios5 sdk - objective-c

I have this case where I am using a custom UITabBar in my application.
It is working great in ios4 on both device and simulator.
But running the same code using xcode 4.2 and ios5 sdk wont work, as the app is crashing when I am setting the view controllers of my custom tabBarController on this line:
tabBarController.viewControllers = [NSArray arrayWithObjects:nc1, nc2, nc3, nil];
Here is how I am creating the tabBar:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
tabBarController = [[MBCustomTabBarController alloc] init];
home=[[Home alloc] initWithNibName:#"Home" bundle:nil];
UINavigationController *nc1=[[UINavigationController alloc] initWithRootViewController:home];
live=[[Live alloc] initWithNibName:#"Live" bundle:nil];
UINavigationController *nc2=[[UINavigationController alloc] initWithRootViewController:live];
report=[[Report alloc] initWithNibName:#"Report" bundle:nil];
UINavigationController *nc3=[[UINavigationController alloc] initWithRootViewController:report];
//custom tabbar items
MBCustomTabBarItem *firstItem = [[MBCustomTabBarItem alloc] initWithTitle:#"headlines" image:[UIImage imageNamed:#"HOME-.png"] tag:0];
MBCustomTabBarItem *secondItem = [[MBCustomTabBarItem alloc] initWithTitle:#"live" image:[UIImage imageNamed:#"NEWS-.png"] tag:1];
MBCustomTabBarItem *thirdItem = [[MBCustomTabBarItem alloc] initWithTitle:#"report" image:[UIImage imageNamed:#"REPORT-.png"] tag:2];
[firstItem setImage:[UIImage imageNamed:#"HOME.png"] forState:UIControlStateSelected];
[secondItem setImage:[UIImage imageNamed:#"NEWS.png"] forState:UIControlStateSelected];
[thirdItem setImage:[UIImage imageNamed:#"REPORT.png"] forState:UIControlStateSelected];
nc1.tabBarItem=firstItem;
nc2.tabBarItem=secondItem;
nc3.tabBarItem=thirdItem;
[firstItem release];
[secondItem release];
[thirdItem release];
tabBarController.viewControllers = [NSArray arrayWithObjects:nc1, nc2, nc3, nil];
[nc1 release];
[nc2 release];
[nc3 release];
tabBarController.customTabBar.frame = CGRectMake(0, 480 - 49, 320, 49);
tabBarController.customTabBar.backgroundColor = [UIColor clearColor];
tabBarController.customTabBar.itemWidth = 320 / [self.tabBarController.viewControllers count];
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
return YES;
}
And here is a small working example.
Your help is highly appreciated.

Never mind, I've totally changed my approach and used this answer to build my custom tabBar. And it is working fine under ios5.

Related

Cannot push back to view controller embed in Navigation Controller

I have created ViewController A (a.k.a vcA) and vcB embed in Navigation View Controller ,mapping child view controller in storyboard as classes.m
When I try to construct this redirect of view controller A->B->A , or to create vcA from vcB , by using :
A :
PairViewController * sliderVC = [self.storyboard instantiateViewControllerWithIdentifier:#"PairViewController"];
sliderVC.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:sliderVC animated:NO completion:nil];
sliderVC.view.backgroundColor = [UIColor clearColor];
B : ViewController *destinationController = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController"];
[destinationController setBle:_ble];
[destinationController setLeDevice:selectedDevice];
destinationController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:destinationController animated:NO completion: nil] ;
The new A is created without any navigation bar appeared.
When I use
[self.navigationController pushViewController:destinationController animated:nil];
The xCode said for the exception ;
Cannot call pushNavigationItem:animated: directly on a UINavigationBar managed by a controller.'
Would you please tell me the way to create vcA from vcB , with my custom navigation bar keeping intact , not missing ?
In A I try to construct the navigation bar in this method but navigation bar turns nil if creating vcA from vcB
-(void)viewWillLayoutSubviews{
navigationBar= [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
}
- (void)viewWillAppear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:NO];
navigationBar = self.navigationController.navigationBar;
[navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
[UIFont fontWithName:#"TitilliumText22L-Medium" size:22.0], NSFontAttributeName,
nil] ];
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:#"FPV Control"];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];
[button setImage:[UIImage imageNamed:#"menu_back.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// UIBarButtonItem *buttonItemA = [[UIBarButtonItem alloc] initWithCustomView:button];
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:#selector(buttonClicked:)];
[leftBarButtonItem setCustomView:button];
navigationItem.leftBarButtonItem = leftBarButtonItem;
UIButton *buttonA = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 20)];
[buttonA setImage:[UIImage imageNamed:#"rc_logo.png"] forState:UIControlStateNormal];
UIBarButtonItem *buttonItemB = [[UIBarButtonItem alloc] initWithCustomView:buttonA];
navigationItem.rightBarButtonItem = buttonItemB;
[navigationBar pushNavigationItem:navigationItem animated:NO];
NSLog(#" navigaytion item : %#" , navigationItem);
NSLog(#" navigaytion bar : %#" , navigationBar);
So present your ViewController with NavigationController as below.
MyViewController *myViewController = [[MyViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navigationController =
[[UINavigationController alloc] initWithRootViewController:myViewController];
//now present this navigation controller modally
[self presentViewController:navigationController
animated:NO
completion:nil];

Shift to UIPopoverPresentationController

I want to create the same popover for my iPhone app with size GGrect of (320 100.0).
Here is my old code:
View * pk1 = [[View alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:pk1];
pop = [[UIPopoverController alloc] initWithContentViewController:nav];
[pop setDelegate:self];
[pop presentPopoverFromRect:[self btnsavepart].frame inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[nav release];
I using this codes but its doesn't change
nav.preferredContentSize = CGSizeMake(320, 100);
I can not change UIpopovercontriller with UIPopoverPresentationController.
How do I specify GGrect of (320 100.0)?
i thinks i can change only on horizontally but how can i do.
View *viewController = [[[View alloc] init] autorelease];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:viewController] autorelease];
navController.modalPresentationStyle = UIModalPresentationPopover;
navController.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
navController.popoverPresentationController.sourceRect = btnsavepart.bounds;
navController.popoverPresentationController.sourceView = btnsavepart;
navController.popoverPresentationController.delegate = self;
[self presentViewController:navController animated:YES completion:nil];
i want the UIPopoverPresentationController not FullScreen form in iPhone
i adding this codes but i give the FullScreen view
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
{
return NO;
}
I work with Xamarin but its same thing , to specify size of the popover in which you want to display your UIViewController, in the properties of your UIVIewController you have the PreferredContentSize property. There you specify the size.
I saw it somewhere here but forgot where.

UITextField keyboard dismiss on button click to UIApplication method

I'm going from storyboard to UISplitViewController in a xib. I want to logout of my storyboard view, which will bring me to the UISplitViewController. In the storyboard controller, I have a UITextField. I set it so that it doesn't dismiss itself after I press enter. However, it now stays open even when I switch to the splitviewcontroller, which I obviously don't want because i want to logout.
Logout Button
UIBarButtonItem *logoutButton = [[UIBarButtonItem alloc] initWithTitle:#"Log Out" style:UIBarButtonItemStylePlain target:self action:#selector(confirmLogout)];
Method to bring up UISplitViewCOntroller
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag==TAG_DEV){
if (buttonIndex) {
[[[UIApplication sharedApplication] delegate] performSelector:#selector(gotoSplitViewController)];
} else {
[self.navigationController popViewControllerAnimated:YES];
}}
}
method gotoSplitViewController in the delegate:
-(void)gotoSplitViewController{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ListViewController *listVC = [[ListViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *listNC = [[UINavigationController alloc] initWithRootViewController:listVC];
AssignedViewController *assignedVC = [[AssignedViewController alloc] initWithNibName:#"AssignedViewController" bundle:nil];
UINavigationController *assignedNC = [[UINavigationController alloc] initWithRootViewController:assignedVC];
//assign the views to the new nav view controllers
splitViewController = [[UISplitViewController alloc] init];
splitViewController.delegate = assignedVC;
splitViewController.viewControllers = #[listNC, assignedNC];
listVC.detailViewController = assignedVC;
[[self window] setRootViewController:splitViewController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
}
my UITextField
self.textBox = [[UITextField alloc] initWithFrame: CGRectMake(10, 660, 750, 90)];
self.textBox.backgroundColor = [UIColor colorWithRed:(209/255.0) green:(236/255.0) blue:(232/255.0) alpha:1];
self.textBox.returnKeyType = UIReturnKeySend;
self.textBox.delegate = self;
[self.view addSubview:self.textBox];
I've tried the resignFirstResponder if various places, including adding
- (void)viewWillDisappear:(BOOL)animated
{
[self.textBox resignFirstResponder];
}
However, none of them work. How do I get the keyboard to disappear after the views change?
I set
self.chatBox.delegate=nil;
in my methods to switch views

Multi view App Programatically

I want to know if it is possible to make multi view app without using the Interface Builder.
I know how to make it with just using Xcode and Storyboards, but I just want to be able make it programmatically.
For example : If I have a UIViewController A (default view) with a UIButton and than I have another UIViewController B with UIWebView so when I click on the UIButton of UIViewController A I want to be able to display the second View (Without IB).
How can I set the action to the UIButton to display my second View (Without Interface Builder) ?
You can add a target to your button:
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
Then in ControllerA, add this method:
-(void)buttonPressed:(id)sender
{
[self.superview addSubview:controllerB.view];
[self.view removeFromSuperview];
}
On ViewControllerA
- (void)viewDidLoad
{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(50.0f,200.0f,60.0f,30.0f)];
[btn setTitle:#"Next" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[btn addTarget:self action:#selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(IBAction)btnPressed:(id)sender
{
NextViewController *nxt=[[NextViewController alloc]initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:nxt animated:YES];
}
On viewControllerB
- (void)viewDidLoad
{
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0, 320, 460)];
NSURL *targetURL = [NSURL URLWithString:#"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
}
Make sure that in AppDelegate.h should be navigation controller 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 = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:self.self.viewController ];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}

How can I create unique PopoverView

How can I create special popOver?
I have manually created a new class, with needed design.
Then I want to load it like a PopOver
-(void) buttonAction {
UIViewController* popoverContent = [[UIViewController alloc] init];
myThirdPop * showHere;//Created class which I load as Popover
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"myThirdPop" owner:self options:nil];
showHere = [nib objectAtIndex:0];
popoverContent.view = showHere.myView;
popoverContent.contentSizeForViewInPopover = CGSizeMake(300, 350);
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
[popoverController presentPopoverFromRect:myButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
But when I trying to tap the button, my app crashes with exc_bad_access
What`s wrong?
Use this code to create the popover
-(IBAction)Click_event
{ UIPopoverController *popoverview;
if(![popoverview isPopoverVisible])
{
Popview *pop = [[Popview alloc] initWithNibName:#"Popview" bundle:nil];
popoverview = [[UIPopoverController alloc] initWithContentViewController:pop];
[popoverview setPopoverContentSize:CGSizeMake(600.0f, 500.0f)];
[popoverview presentPopoverFromRect:CGRectMake(400, 400, 0, 0) inView:self.Click_but permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
else
{
[popoverview dismissPopoverAnimated:YES];
}
}
Lets try this. It may be helpful for you
Try this way u may fix the issue....
PopimagepickerViewController.h
UIPopoverController *popoverController;
UIPopoverController *popoverimagview; // imagepicker popoverview
PopimagepickerViewController.m
-(IBAction)popbtn_Click:(id)sender
{
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,230,180)];
popoverView.backgroundColor = [UIColor whiteColor];
popoverContent.view=popoverView;
popoverContent.contentSizeForViewInPopover = CGSizeMake(230, 180); // Set the popoverview Width and height
//create a popover controller
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
CGRect popoverRect = [self.view convertRect:[popbtn frame]
fromView:[popbtn superview]];
popoverRect.size.width = MIN(popoverRect.size.width, 100) ;
popoverRect.origin.x = popoverRect.origin.x;
[popoverController
presentPopoverFromRect:popoverRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionUp +
UIPopoverArrowDirectionLeft //pooverview down direction
animated:YES];
[popoverView release];
[popoverContent release];
}
Your code seems overly complex, and non standard. I use this code to create a popover.
(I've edited this to match your case, but it's not tested)
-(void) buttonAction:(id) sender
{
UIViewController *myThirdPop = [[NSBundle mainBundle] loadNibNamed:#"myThirdPop" owner:self options:nil];
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:myThirdPop];
[popoverController presentPopoverFromRect:[sender bounds] inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[myThirdPop release];
}