Correctly link my buttons to my views (in UINavigationController)? - objective-c

I'm sorry if this question sounds kinda newbie, but I'm a beginner and want to improve. So, I have a Navigation Controller embedding my whole application. My main view has a button, linking to another view.
Here is what I did in my main view:
.h
#property (nonatomic, retain) IBOutlet UIButton *button;
In .m, some code for my button.
In IB, I've added a button in my main view and another ViewController whose I changed the class. And the following links :
button -> Button
button -> OtherViewController (Push)
Now my question is, what do I miss to add in my code or IB ? Do I need an IBAction too ?
Thanks a lot for your advices..

-(IBAction)clickButton:(id)sender{
if (!createViewController) {
createViewController = [[CreateViewController alloc] initWithNibName:#"CreateViewController" bundle:nil];
}
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
[self.navigationController pushViewController:createViewController animated:YES];
}

Yes, you need a method that defines the action for your button.
In .h:
- (IBAction)buttonPressed:(id)sender;
In .m:
- (IBAction)buttonPressed:(id)sender { // what you want to do when the button is pressed }
Then click on the button from your interface, from Connection Inspector, control click the radio button from "Touch Up Inside", connect to File's Owner and select the method -buttonPressed.

Related

How can I modify an NSButton that I dragged from the library in my code?

I dragged a button from the library to the window. I have the button linked to an IBAction in the AppDelegate.h, and now I want to change the title of the button in the AppDelegate.m file, how can I access that button, should I type "self.window....setTitle:" or whatever? Thanks!
If your IBOutlet to the NSButton is myButton then use
[self.myButton setTitle:#"New Title"];
or even
[_myButton setTitle:#"New Title"]; //If you are using XCode4.4+ as #synthesize will be there creating _myButton.
As you said you have connected the button to an IBAction then you can simply do :
-(IBAction)buttonClick:(id)sender{
sender.title=#"New Title";
}

UIView inside a UIViewController or better way to do it?

I have a problem on how to properly do a certain kind of action.
The image below shows a UIViewController, but the second part of the view is a custom UIView (the one with the profile pic, name and Show View button).
The subclassed UIView is allocated using this code:
profileView = [[GPProfileView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 70)];
profileView.myTripGradientColor = YES;
[self.view addSubview:profileView];
The problem is of course, that the button on the UIView can't show any view, since it's only the UIViewController that can push another ViewController to the window(correct?).
The UIView is used in several places in the app and needs to be added easily and have the same behavior across the application.
This worked great until I added the button, and I'm starting to think I've made this wrong, and there has to be a better way to do it (maybe change the UIView to something else?).
I was thinking I should be able to call:
self.superview
And then somehow get the ViewController so I can push another ViewController into the view hierarchy, but nope.
Any suggestions and a tips on how to do this correctly?
UPDATE:
I have no idea on how to push another UIViewController from the button.
What should I do in this method when pressing the button in the UIView:
- (void) showViewButtonTouched:(UIButton*)sender {
GPProfileSocialFriendsViewController *friendsSettings = [[GPProfileSocialFriendsViewController alloc] init];
}
How do I push GPProfileSocialFriendsViewController?
Your - (void) showViewButtonTouched:(UIButton*)sender method should be in your controller and would probably be better named - (void) showView:(UIButton*)sender or - (void) showProfile:(UIButton*)sender so it clearly denotes what it does (not how you got there).
It's not the view's responsibility to manage transitions from a state to another. If you move your method to your controller, your problem is no more (you can easily access self.navigationController or push directly if you don't have an navigation controller like this:
[self presentViewController:vcThatNeedsToBePushed animated:YES completion:nil];
I think you can create weak reference in GPProfileView on UIViewController. Like this:
#property (weak, nonatomic) UIViewController *rootController;
when you create GPProfileView, assign rootController-property:
profileView = [[GPProfileView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 70)];
profileView.myTripGradientColor = YES;
profileView.rootController = self; // if view created in view controller
[self.view addSubview:profileView];
and in implementation of button selector:
self.rootController push... // or pop
May be this not correct, but you can try
You could let the view controller push the next view controller when the button is pushed. The view controller can add a target/action on the button, so that the action method in the view controller is called on the touch up inside event.

How to handle seque if we use storyboard and use external XIBs

In story board we just right drag the button to it's destination and create a seque.
What about if I have external XIBs for all those viewControllers in storyboard? I can't drag the button again. The button is in external XIBs, not in storyboard anymore.
So what should I do?
You should to create an IBOutlet for this button. For example, it's name "buttonPressed". And then implement this method.
- (IBOutlet)buttonPressed
{
YourViewController *yourViewController = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
[self.navigationController pushViewController:yourViewController animated:YES];
}

Objective-C: Adding UIButtons programmatically from another class

I'm having trouble connecting the dots between code and .xib files.
If I want to add a series of UIButtons programmatically to my view controller, how would I go about doing this from a separate class?
For instance, if I have MainViewController.m, which is set as the root view controller in Xcode, how can I add a UIButton to that view controller from SecondViewController.m? Is this even possible?
I would essentially like to place all of my "user interface" code in a separate class.
Thanks
To do this, create a UIButton *myButton programmatically and then call [mainViewController addSubview:myButton];. This may mean you need to store a MainViewController * property in your SecondViewController class.
Important methods and properties for a UIButton instance (essentially, just take a look at the documentation, but here's a minimal set of stuff to get you started):
+[UIButton buttonWithType:buttonType] - Make sure if you're doing anything remotely custom to use UIButtonTypeCustom here (it doesn't give you any default background images or otherwise to have to nil out)
setFrame: - Position the button relative to its container and set the size, for usability reasons the width and height should be at least 44 pixels (as mentioned here).
setTitle:forState: - UIControlStateNormal will act as the default properties for other states too, so you may only need to set the text here
setBackgroundImage:forState: - use UIControlStateNormal and UIControlStateHighlighted/UIControlStateSelected primarily, UIControlStateDisabled if you wish to show it grayed out or inaccessible at any point.
setImage:forState: - Use for an icon next to the button text (like an arrow pointing down for Save or up for Load, etc)
setEnabled:, setHidden:, setSelected: - Transition between different button states. setHighlighted: happens automatically when you tap the button.
addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside - TouchUpInside is almost always what you want for a simple button press, I'm using a method named buttonClicked: here to handle my button press.
Oh, and if you use [[UIButton alloc] initWith...] don't forget to [myButton release] once it's added to the mainViewController :)
use this
#import "MainViewController.h"
#interface SecondViewController
{
MainViewController *mainView;
}
#property(nonatomic, retain) MainViewController *mainView;
-(void)addButtons;
In your implementation
#synthesize mainView;
-(void)addButtons
{
UIButton *add = [UIButton alloc] init];
//do necessary stuff on button here
[self.mainView addSubview:add];
[add release];
}
In your MainViewcontroller.m
#import "SecondViewController.h"
-(void)viewDidLoad
{
[self superViewDidLoad];
SecondViewController *second = [SecondViewController alloc] init];
second.mainView = self;
[second addButton];
[second release];
}

Reset a Modal View Controller

I have a modal view controller that initiates a process. If I dismiss the modal view controller, then reopen it, it continues from where it left off. What I want is to (upon dismissal) reset the modal view controller, cancel all processing and reset it to it's initial state. Is there a way?
Cheers
Don't save the UIViewController subclass object in an ivar, just alloc] init] a new one every time you want to present one.
sorry if I'll say something obvious, but I spent some of time to figure out how to do this:
Don't save the UIViewController subclass object in an ivar, just alloc] init] a new one every time you want to present one.
In my case I have this code in MasterViewController.h
#property (strong, nonatomic) ContactsDetailViewController *detailViewController;
and this one in MasterViewController.m
#synthesize detailViewController = _detailViewController;
if (!self.detailViewController) {
self.detailViewController = [[ContactsDetailViewController alloc]
initWithNibName:#"ContactsDetailViewController"
bundle:nil];
}
You should delete this code and use next code in place, where you pushing your modal view controller:
ContactsDetailViewController *detailViewController = [[ContactsDetailViewController alloc] initWithNibName:#"ContactsDetailViewController" bundle:nil];
detailViewController.title = #"View Controller"; // for example
[self.navigationController pushViewController:detailViewController animated:YES];
note: I'm using ARC in this project
Hope it will be helpful for someone
IN the viewDidUnload method of the viewController for your modal view, try stopping the task.