Simultaneous push ViewController - ios

I have a "strange" issue with xcode5 and the new sdk.
I have a Navigation controller.
The root view controller has 2 buttons, both buttons are linked to an action that push a viewcontroller into navigation-controller's stack.
If I run this project on XCode5 and if I push both buttons simultaneous the navigation controller going "crazy" with the message: "nested push animation can result in corrupted navigation bar".
But if I try the same code with xCode4 the application work also if I push simultaneous.
Have I forgot something??
Is this a already known behaviour?
There are a way to fix this issue?
Thanks a lot
This is the simple sample code
'
-(void)viewDidLoad
{
[super viewDidLoad];
UIButton *BTN1= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[BTN1 addTarget:self action:#selector(ACTION) forControlEvents:UIControlEventTouchUpInside];
[BTN1 setTitle:#"XXX" forState:UIControlStateNormal];
[BTN1 setFrame:CGRectMake(0, 100, 100, 100)];
UIButton *BTN2= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[BTN2 addTarget:self action:#selector(ACTION) forControlEvents:UIControlEventTouchUpInside];
[BTN2 setFrame:CGRectMake(0, 200, 100, 100)];
[BTN2 setTitle:#"XXX" forState:UIControlStateNormal];
[self.view addSubview:BTN1];
[self.view addSubview:BTN2];
}
-(void)ACTION
{
FirstViewController *fi = [[FirstViewController alloc] init];
[self.navigationController pushViewController:fi animated:YES];
}`

To prevent the navigation stack from being corrupted when pressing 2 buttons simultaneously, it is best to set exclusive touch to the buttons you are working with. This way you can prevent multiple triggers of the button action and prevent multiple ViewControllers from being pushed to the stack and corruption to happen.
[yourButton setExclusiveTouch:YES];

Use button.exclusiveTouch = YES; on each of your buttons.
You will need to hook them up to UIButtons and set the property in viewDidLoad for example.

Related

#Selector nothing happen

I Know this is probably very easy question, but i can't do that, i am using #selector to dismiss a view, writing this code, nothing happen:
- (void)viewDidLoad {
//here create my button
dismiss = [UIButton buttonWithType:UIButtonTypeRoundedRect];
dismiss.frame = CGRectMake(100, 10, 60, 50);
[dismiss setTitle:#"Close" forState:UIControlStateNormal];
[dismiss setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[dismiss addTarget:self action:#selector(closeView) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:dismiss];
}
//here the function
- (void)closeView {
[self dismissViewControllerAnimated:YES completion:nil];
}
But nothing happen, where am I doing mistake?
thanks
Try writing the addTarget line of code after [self.view addSubview:dismiss]; and check
That depends on what you're trying to do. dismissViewControllerAnimated:completion: will dismiss a controller that has been presented with presentViewController:animated:completion:, if there is no controller presented, it won't do anything.

link button to view controller programmatically

I am starting with xcode and objective C, maybe my quiestions are basics even repetitive but really already I was looking, please no negative votes n_n
I want to link a button to view controller
Graphically with Xcode I can make it but I need to do it programmatically
button is created with the following code
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn1.frame = CGRectMake(40, btnp, 100, 30);
btn1.backgroundColor = [UIColor purpleColor];
[btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn1 setTitle:#"Ingresar" forState:UIControlStateNormal];
[self.scrolling addSubview:btn1];
this button is in principalviewcontroller
then I wanna that
when I click the button a new viewcontroller named "viewcontroller" is opened
how to I do?
Already I was looking and looking and looking but I have not found a solution
I have several days with this, help me please
[btn1 addTarget:self action:#selector(showSecondViewController) forControlEvents:UIControlEventTouchUpInside];
then create the method
- (void) showSecondViewController {
SecondViewController * secondVC = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secondVC animated:YES];
}

How to make buttons that go to a different screen?

Hey I'm having a lot of trouble with this idea I have. How do I make buttons that go to a different screen but on the CODE?
Basically depending on a value that may or may not be different for every user (Let's say the value is in x already) it will make a list of buttons...
When you click the list of buttons, they each individually go to a different screen.
How do I do this? I know how to on storyboard.. but code wise?
Edit: I SHOULD note that for the buttons specifically I have this and it is working in the code (didn't use storyboard):
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(50, 200, 200, 50);
[btn setTitle:#"Button 1" forState:UIControlStateNormal];
[self.view addSubview:btn];
First add a target to your button:
//Add target to your button, to call a method that presents the next ViewController:
UIButton *yourButton = [[UIButton alloc]init];
[yourButton addTarget:self action:#selector(goToNextPageMethod) forControlEvents:UIControlEventTouchUpInside];
User one of the two, case you are using a UINavigation controller or not, place them in the method you are calling when the button is pressed (the target)
//Normal presentation
YourViewController *vc = [[YourViewController alloc]init];
[self presentViewController:vc animated:YES completion:nil];
//Navgiation bar
YourViewController *vc = [[YourViewController alloc]init];
[self.navigationController pushViewController:vc animated:YES];
Hope this helps
You can attach an action to your button by doing the following:
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(50, 200, 200, 50);
[btn setTitle:#"Button 1" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(someAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
and then in your someAction method, you push the view modally or via your navigation controller
- (void)someAction
{
//init your view controller here....
YourViewController *vc = [[YourViewController alloc]init];
[self.navigationController pushViewController:vc animated:YES];
}

Modal View Controllers - UIBarButtonItems Do Not Persist

I present modal view controllers, packaged in a UINavigationController.
When they first appear, the bar button items are alright.
However, if I click a button and a new view controller is pushed to the UINavigationController, and then I use the back button of that view controller, and it gets popped back to the original modal view controller, the buttons do not show up, (but they work when I click them -> just can't see them)
I can't seem to figure out why this occurs.
Here's some code.
Oh, and by the way, I customize the navigation controller's navigation bar's background. I have a feeling this may be causing this visual interference, but I don't know what to change to get it right.
In GGMainViewController:
GGSelectTeamsViewController *chooseTeam = [[GIFSelectTeamsViewController alloc] init];
UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:chooseTeam];
[self setupModalNavigationBar:navigationController];
[self presentViewController:navigationController animated:YES completion:nil];
- (void)setupModalNavigationBar:(UINavigationController *)nav {
// unnecessary code removed for a quicker read
// basically navigation bar has a background gradient as well as a bottom border
[nav.navigationBar.layer addSublayer:bottomBorder];
[nav.navigationBar.layer addSublayer:gradient];
}
In GGSelectTeamsViewController
- (void)viewDidLoad
{
[super viewDidLoad];
title = [[UILabel alloc] init];
title.text = someText;
title.backgroundColor = [UIColor clearColor];
title.textColor = [UIColor whiteColor];
self.navigationItem.titleView = title;
[title sizeToFit];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
backButton.frame = someFrame;
[backButton addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:backButton] animated:NO];
UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
[selectButton setTitle:#"Select" forState:UIControlStateNormal];
[selectButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
selectButton.frame = someFrame;
[selectButton addTarget:self action:#selector(sendContent) forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:selectButton] animated:NO];
}
Since the navigation items are set in the viewDidLoad method, why can't I see them (but they are still functional) when I return to it from another view controller?
That seems like an over-complicated way of doing this.
Why don't you create your own button and then use the setRightBarButtonItems:items: method from the UINavigationBar to set the buttons you want?
I'm guessing they disappear because you're adding them to the layer of the UINavigationBar.
Still, if you want to keep this method with the layers, you may want to add them in the viewWillAppear method, but you may need call the removeFromSuperlayer to make sure you don't add stuff more than once.
Hope this helps.
The problem was:
the gradients for the navigation bar (done by code)
The solution was:
use Sketch to create an image of the gradients and use the appearance proxy to set the image as a background image
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"NavigationBarBackground"] forBarMetrics:UIBarMetricsDefault];

Subclassing UIView/UIViewController

I need a lot of custom buttons in my app. At the moment I'm using storyboard to define these buttons on every controller that I need. However, I feel that since I need them throughout my app, I'm better off putting in a different view controller/view that subclasses UIView or UIViewController so if I need to make any changes I will just have to do them once as opposed to in different view controllers. What would be the best way to do this? Also can you tell me how can I create buttons programatically? This is what I'm doing at the moment, and I'm getting a completely blank screen.
-(void)viewDidAppear:(BOOL)animated
{
UIButton *testButton = [UIButton buttonWithType:UIButtonTypeCustom];
[testButton setFrame:CGRectMake(0, 390, 100, 40)];
[testButton setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
[setImage:[UIImage imageNamed:#"test_button"] forState:UIControlStateNormal];
[setImage:[UIImage imageNamed:#"test_button"] forState:UIControlStateHighlighted];
[setEnabled:YES];
[self.view addSubview:testButton];
}
there is a chance that your button is way off the visible rect. for adding the button programmatically, you are doing it mostly right. also, images should have the file extension. since you are adding a customButton, if there are no images, then you wont see any button. Try adding a title to your button, which would show up even if there is no image added.
try the code below.
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated]; //you must have a good reason to not add this line.
UIButton *testButton=[UIButton buttonWithType:UIButtonTypeCustom];
[testButton setFrame:CGRectMake(0, 20, 100, 40)];
[testButton setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
[testButton setImage:[UIImage imageNamed:#"test_button.png"] forState:UIControlStateNormal];
[testButton setImage:[UIImage imageNamed:#"test_button.png"] forState:UIControlStateHighlighted];
[testButton setEnabled:YES];
[self.view addSubview:testButton];
}

Resources