Xcode storyboard how to detect touch item - objective-c

I have a storyboard setup (the default) with the tabbed application template.
I want to add a BarItem and have that launch a URL when touched. I don't want it to go to a view in the app. How can I do this using the storyboard?
I am new the story board concept and not sure what to add to the story board to add a new bar item and then make it call a method.

First you need to put a function in the ViewController code something like...
- (IBAction)barButtonPressed:(id)sender
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com"]];
}
Then go add the button to the view controller in the Storyboard and ctrl click drag from the button to the function.
This will make the button run that function when pressed.

If you mean the Navigation bar
//in viewDidLoad or wherever
UIBarButtonItem *openWebLink = [[UIBarButtonItem alloc]
initWithTitle: #"OpenWebLink"
style: UIBarButtonItemStyleBordered
target: nil action: #selector(openLink)];
[self.navigationItem setBackBarButtonItem: openWebLink];
-(void) openLink
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com"]];
}
If you don't, look at Fogmeister answer
Code not tested!

Related

IBAction that create a new UIViewController that is identical to the current one

I am developing a simple drawing app that saves the user's change (paint) then allow the user to have a new empty page when clicking on "New Page" button.
I'm facing a problem while trying to create a new page..
I'm using the single view template and currently my app contains only one ViewController. I want a new ViewController that is identical to the current one (but without the user's change (the user paint)) to be generated automatically when clicking on the "new page" button.
It is kind of similar to the notepad app, except that it doesn't use a tableView. I've looked over a lot of NotePad app source code, but I can't find what I actually need!
I hope you can help
thanks in advance
try this:
- (IBAction)newPagePressed
{
UIViewController *newVC = [[self class] alloc] init];
[self.navigationController setViewCntrollers:#[newVC]];
}
you'll need to use UINavigationController to achieve it, otherwise you can try:
- (IBAction)newPagePressed
{
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
UIViewController *newVC = [[self class] alloc] init];
appDelegate.rootViewController = newVC;
}

xcode navigation button url

I am trying to add a "Home" button to my app, where if you click on a button in the toolbar, if will take you to a specific url "google.com".
So I can put a button in the nav bar, and make it say home.
and I can put this in the viewcontroller.m:
-(void)openGoogleURL
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.google.com"]];
}
Is what I put in viewcontroller correct? How do I hook up my button?
UINavigationController uses a UINavigationItem object of each UIViewController to display the items in navigation bar. So you can add the button you want in the bar by doing something like this on your View Controller:
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:#"Home" style:UIBarButtonItemStylePlain target:self action:#selector(openGoogleURL)]];
That way the button target-action will be linked with the function you've defined in your UIViewController.
Note that for every view controller you want to add this button, you'll need this code.
You can wire the button click event to your code either programmatically or by using Interface Builder. Here's some documentation from the Apple's website: https://developer.apple.com/library/mac/#documentation/General/Conceptual/Devpedia-CocoaApp/TargetAction.html

Hide status bar for entire app

I create views programmatically. To hide status bar in view I use
[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
in viewDidload method. The problem is every view have to implement the code above to be status bar hidden. Is there a way (programmatically) to set status bar hidden just in one place in the app so entire app to be without the status bar ?
I have tried to add this in AppDelegate, but it doesn't work.
Open your app plist file MyApp-Info.plist and add a row with the Status bar is initially hidden and the YES value.
EDIT:
If you want to do it programmatically, add this in your ApplicationDidFinishLaunching :
[UIApplication sharedApplication].statusBarHidden = YES;
If you are targeting the devices with iOS > 3.2, then use the following code in application:didFinishLaunchingWithOptions: method in AppDelegate class.
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
just put key "Status bar is initially hidden" as YES in Info.plist file.
you will get hide status bar throughout the application.
If you want to do it by problematically, then just put this code in Appdelegate.m file of your project.
[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
Instead of creating new views based on UIView, subclass UIView (we can call it SummercView) and add a viewDidLoad method to it that looks like:
- (void) viewDidLoad
{
[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
[super viewDidLoad];
}
And then in your xib or storyboard files, set the views where you want to hide the status bar to use SummercView instead of UIView.
And of course #Aadhira's answer is good, too. +1 to him/her.
Couldn't you create a view class which did this in viewDidLoad, and have your views be subclasses of it? They'd still each have to hide the status bar, but at least you wouldn't have to duplicate the code in each subclass.

viewController management programmatically

I have a problem about viewController. I created a program What is viewController based applicaiton. There is 4 button on mainViewController. I used this code for calling mainviewController
-(void) applicationDidFinishLaunching:(UIApplication *)application{
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
}
Then click to any button on homePage , I go to the other viewController. This code for call another viewController belong
-(IBAction)clickCalendarButton{
calendarButton.selected=YES;
[calendarButton
setImage:[UIImage imageNamed:#"afvalkalender_pressed.png"] forState:(UIControlStateHighlighted+UIControlStateSelected)];
GarbageCalendar *garbageCalendar = [[GarbageCalendar alloc] initWithNibName:#"GarbageCalendar" bundle:nil];
[self presentModalViewController:garbageCalendar animated:YES];
}
And then I want to go home page from another viewController. But I didn' go home page viewController.
Create button on detail view controller, which calls something like this:
- (IBAction)goBack {
[self dismissModalViewControllerAnimated:YES];
}
If you want to keep your current UI design, based on modal view controllers, then I think you should ensure that your other view controllers have got a button that does the dismiss of the view. Say, e.g., a "Back" or "Done" button. When you click on that button, a delegate method is called that executes: [self dismissModalViewControllerAnimated:YES];
Look also at this document for more info, section "Dismissing a Modal View Controller".
If you would like to consider alternative approaches to your UI, you could look into using a UINavigationController, which would make your life a little bit easier with navigating back from one controller to another.

Hide the tab bar in a tab bar application

I have created a new project from the template:
IPhoneOS>Application>Tab Bar Application.
I get two tabs.
How can I make the second become a full screen hiding the tab bar and even the status bar?
I tried to check the "Wants Full screen" - but it didn't help.
(Much less important... When I do get a full screen I do I get back?)
Please give me a simple code/guidelines or a reference to them, cause I'm a beginner - and Me and the compiler got too many issues to make things worse
Thanks
Asaf
To hide the tab bar you can use hidesBottomBarWhenPushed. For instance:
MyController *myController = [[MyController alloc]init];
myController.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:myController animated:YES];
[myController release];
To hide the status bar you can use:
[[UIApplication sharedApplication] setStatusBarHidden:YES];
To hide the nav bar you can use:
self.navigationController.navigationBarHidden = YES;
You can just use:
//Navigation bar:
self.navigationController.navigationBarHidden = YES;
//Statusbar:
[[UIApplication sharedApplication] setStatusBarHidden:YES];
//Tabbar:
self.tabBarController.tabBar.hidden = YES;
Have you checked Modal View Controllers out?
http://developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html
Try the presentModalViewController:animated: method on your navigationController (instead of pushing a view controller)
[self.navigationController presentModalViewController:foo animated:YES];
Another way to accomplish this is by making the UITabBarController the rootViewController of a UINavigationController. Then when you pushViewControllerAnimated: the tab bar will slide away with the root view controller.