iOS xcode Button isn't linking. - ios

I have a one button on my main storyboard. I'm pretty sure it's linked up correctly but when I run the simulator and hit the button, it does nothing. What am I missing to get the button to open Safari?
Viewcontroller.h
#import <UIKit/UIKit.h>
#interface testViewController : UIViewController
- (IBAction)openSafari:(id)sender;
#end
Viewcontroller.m
#import "testViewController.h"
#interface testViewController ()
#end
#implementation testViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)openSafari:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"www.google.com"]];
}
#end

A URL needs a method as well as an address, so try #"http://www.google.com". To prove whether your action is hooked up right, print a log message from the method (or use a breakpoint).

Related

iOS SDK - Track viewDidLoad method

I am working on a SDK, where I will have to make a call to our server whenever viewDidLoad method of any UIViewController is called in the app integrated with our SDK. I am trying to use Categories as shown below:
#import "DymmyViewController.h"
#interface DymmyViewController ()
#end
#protocol DummyDelgate;
#interface UIViewController (DummyAddition)
-(void)viewDidLoad;
#end
#implementation DymmyViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
#end
#implementation UIViewController (DummyAddition)
-(void)viewDidLoad{
//server call
}
#end
Nothing happens with this and I get a warning saying "Category is implementing a method which will also be implemented by its primary class"
I understand this is not the way to get this done. Is there any other way I can do this? May be using NSNotification?
This is the way to use protocols and delegates. Delegated are good techniques to respond from one class to another class.
DummyView.h
#import <UIKit/UIKit.h>
#class DummyView;
#protocol DummyViewDelegate <NSObject>
- (void)addItemViewController;
#end
#interface DummyView : UIViewController
#property (nonatomic, weak) id <DummyViewDelegate> delegate;
#end
DummyView.m
#import "DummyView.h"
#interface DummyView ()
#end
#implementation DummyView
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)backButtonClicked:(id)sender {
[self.delegate addItemViewController];
}
ViewController.h
#import <UIKit/UIKit.h>
#import "DummyView.h"
#interface ViewController : UIViewController <DummyViewDelegate>
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
{
DummyView *acController;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
acController.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)addItemViewController{
}
Why not just make a tracking view controller available in the SDK. The app dev should then make all of their view controllers of this base type. You might need a couple of base classes (e.g. if you're also using TableViewControllers).
In SDK:
#interface TrackingViewController : UIViewController
#end
#implementation TrackingViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Make the call here
// ....
}
In app:
#import "SDK.h"
#interface MyViewController : TrackingViewController ...

Viewcontroller Dealloc Method Never Gets Called

I know there are many questions like this. I read all. My problem is very simple.
I created a single view app from xcode file>new project>single view app.
Then i added a second uiviewcontroller in storyboard and a new viewcontroller class named secondViewController. I dragged a button to main viewcontroller and by ctrl+drag to secondViewController on storyboard. I did the reverse in secondViewController. And just added dealloc functins with nslog to class files. I also added weak references to uibuttons
Dealloc method of each viewcontroller never gets called when view changes.
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"viewDidLoad 1");
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
NSLog(#"dealloc 1");
}
SeconViewController.m
#import "SecondViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
NSLog(#"dealloc 2");
}
#end
ARC is enabled.
Zombies seem to be disabled on product>edit scheme. I am using xcode 6.2. In instruments allocation screen memory rises at each toggle.
What is the problem, I couldnt find?
dealloc calls when object's (Here its viewcontroller object) swipe out from memory.But here in your case you must presenting view controllers from one another that leads to call only viewwilldisappear and diddisappear.
In storyboard if you want to remove those view controllers completely from memory u should call unwind segue

How to give UIWebView access to current gps location

I have a UIWebView inside my app, this loads a website which is trying to get the current gps location of the user. On Safari it presents a popup which asks if you want to give your current location to the website, on my UIWebView no such thing happens and my website is not able to get the gps location of the user. How can I accomplish this? I am new to ios developing so bear with me if this is something very easy to do, but I couldnt find an example so far.
Here is what I have so far:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UIWebView *_webView;
}
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[_webView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:#"http://google.com"]]];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)goBack:(id)sender {
if ([_webView canGoBack]) {
[_webView goBack];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Edit your app's info.plist and set
NSLocationWhenInUseUsageDescription "String you want to explain why you need location."

Passing data between ViewControllers is working, but not with Tab Bar Controller

Could anyone please help me in the following problem?
I new in Xcode, I'm still learning it, but I could figure out how to pass data between ViewControllers.
Here is my working code:
FirstViewController.h:
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController
- (IBAction)displayView:(id)sender;
#property (weak, nonatomic) IBOutlet UILabel *lblFirst;
#end
FirstViewController.m:
#import "FirstViewController.h"
#import "SecondViewController.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
#synthesize lblFirst;
SecondViewController *secondViewController;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondViewController.forwarded_lblFirst = lblFirst;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)displayView:(id)sender {
[self.view addSubview:secondViewController.view];
}
#end
SecondViewController.h:
#import <UIKit/UIKit.h>
#interface SecondViewController : UIViewController
#property (nonatomic, retain) UILabel *forwarded_lblFirst;
#property (weak, nonatomic) IBOutlet UITextField *txtSecond;
- (IBAction)btnReturn:(id)sender;
- (IBAction)txtSecond_DidEndOnExit:(id)sender;
- (IBAction)btnSecond:(id)sender;
#end
SecondViewController.m:
#import "SecondViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
#synthesize forwarded_lblFirst;
#synthesize txtSecond;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btnReturn:(id)sender {
[self.view removeFromSuperview];
}
- (IBAction)txtSecond_DidEndOnExit:(id)sender {
forwarded_lblFirst.text = txtSecond.text;
[txtSecond resignFirstResponder];
[self.view removeFromSuperview];
}
- (IBAction)btnSecond:(id)sender {
forwarded_lblFirst.text = txtSecond.text;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[txtSecond resignFirstResponder];
}
#end
This code is working perfectly, the text what I enter on the SecondViewController's textbox (after pressing the btnSecond or simply the Return key on the keyboard) appears as the text of the Label on the FirstViewController.
My problem is, that when I do the exact same thing, but with a Tab Bar Application, the Label on the First tab won't change.
I can use the exact same code (except the changing-views part, because I handle on the first app with programmed buttons, but for the Tab-Bar-App there are already the buttons), there are also two ViewControllers for the Tab Bar App, too (one for each tabs).
So the code is the same, line by line, character by character, and the files are the same, too, for both apps (View-Based, Tab-Bar).
Why isn't my code working? How can I solve this problem?
Thank you!
You need to read some articles about transferring data between views:
Passing Data between View Controllers
Storyboard
passing data between views
iPhoneDevSDK
Talking about your problem, try this approach:
Add the property to your Application Delegate.
When assigning the property do something like:
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
delegate.myProperty = #"My Value";
then, in your different tabs, you can retrieve this property in the same manner:
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *valueInTab = delegate.myProperty;
A different approach to this problem is to use an independent data model.
Create an object that provides access and manipulation methods for all the data that your application needs to save, load, share, or use in any non-trivial way. Make the object available either as a Singleton or as a property of the application delegate. When changes happen, have the data model update application state. When something needs to be displayed, have the controller fetch it from the data model and send it to the view (MVC!).
If you don't store things in controllers that actually belong in the model, you never need to worry about passing information from controller to controller.
You need to use tabbardelegate in your secondview controller, assuming that you use this schema
FirstViewController.h
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController
#property (strong, nonatomic) NSString *content;
#end
SecondViewController.m
#import "SecondViewController.h"
#import "FirstViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tabBarController.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
if ([viewController isKindOfClass:[FirstViewController class]]){
FirstViewController *vc =( FirstViewController *) viewController;
vc.content = #"your content";
}
return TRUE;
}

Crash on master/detail app, when changing category, after releasing Var

I'm struggling to figure out why my app is crashing when I release synthesized properties. My app launches, and when I tap on a row, it takes me to DetailViewController, then when I go back and tap a row again the app crashes with EXC_BAD_ACCESS.
DetailViewController.h:
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController {
IBOutlet UILabel *clipboardLabel;
}
#property (nonatomic, retain) IBOutlet UILabel *clipboardLabel;
#end
DetailViewController.m
#import "DetailViewController.h"
#implementation DetailViewController
#synthesize clipboardLabel;
- (void)viewDidLoad
{
// Do any additional setup after loading the view from its nib.
clipboardLabel.text = #"Tap an image to copy";
[super viewDidLoad];
}
- (void)dealloc
{
[clipboardLabel dealloc];
[super dealloc];
}
#end
Call release instead of dealloc on your clipboardLabel in the dealloc method.
That should be :
- (void)dealloc
{
[clipboardLabel release];
[super dealloc];
}
A general rule : one should never call deallocon another object.
Do not call dealloc:
[clipboardLabel dealloc]; <-- Wrong
call release:
[clipboardLabel release]; <-- Right

Resources