I have a tabbar project in xcode and on the first view i need to find my GPS location and I need to save longitude and latitude on two variables on the appdelegate. Here some code:
FirstViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface FirstViewController : UIViewController <CLLocationManagerDelegate>{
CLLocationManager *locationManager;
}
#property (nonatomic, retain) CLLocationManager *locationManager;
#end
FirstViewController.m
#import "FirstViewController.h"
#import "CampeggiandoAppDelegate.h"
#import <CoreLocation/CoreLocation.h>
#interface FirstViewController ()
#end
#implementation FirstViewController
#synthesize locationManager;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.tabBarItem.image = [UIImage imageNamed:#"ic_home2"];
self.tabBarItem.title=#"Home";
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self;
self.locationManager.distanceFilter=500.0f;
self.locationManager.desiredAccuracy=kCLLocationAccuracyHundredMeters;
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
CampeggiandoAppDelegate *appDelegate=(CampeggiandoAppDelegate*)[[UIApplication sharedApplication]delegate];
appDelegate.latitudineDel=[NSString stringWithFormat:#"%3.5f", newLocation.coordinate.latitude];
appDelegate.longitudineDel=[NSString stringWithFormat:#"%3.5f", newLocation.coordinate.longitude];
}
When I run the compiler and the fitst view comes up the application broke and appears this exception:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[AppDelegate
setLatitudineDel:]: unrecognized selector sent to instance 0x84210b0'
Any help? Thanks.
Your class interface should look like this before setting variables the way you do:
#interface CampeggiandoAppDelegate : UIResponder <UIApplicationDelegate> {
// ...
// some ivars
// ...
}
#property (nonatomic, strong) NSString *latitudineDel;
#property (nonatomic, strong) NSString *longitudineDel;
#end
What you have for this moment is this:
#interface CampeggiandoAppDelegate : UIResponder <UIApplicationDelegate> {
// ...
NSString *latitudineDel;
NSString *longitudineDel
// ...
}
#end
So there are no setters for those instance variables, that's why the exception is thrown.
For more information about properties read this.
Related
My cpViewController has a button that modal segues to cpScanViewController. I want to have cpScanViewController inform ViewController with a string when it has completed a successful scan. After reading numerous articles online, I believe delegation is the best way to do this?
Is the delegate being set correctly?
How does the delegate inform the cpViewController?
cpScanViewController.h
#import <UIKit/UIKit.h>
#protocol ScanDelegate <NSObject>
-(void)receivedUPC:(NSString *)Scan;
#end
#interface cpScanViewController : UIViewController
#property (nonatomic, weak) id <ScanDelegate> delegate;
#property (nonatomic, retain) NSString *UPC;
-(void)checkUPC;
#end
cpScanViewController.m
#interface cpScanViewController ()
{
NSString *UPC;
}
#end
#implementation cpScanViewController
#synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(void)doSomeScanStuff
{
UPC = #"a string"
// even tried
[delegate receivedUPC:UPC];
}
-(void)checkUPC
{
}
#end
cpViewController.h
#import <UIKit/UIKit.h>
#import "cpScanViewController.h"
#interface cpViewController : UIViewController <ScanDelegate>
#end
cpViewController.m
#interface cpViewController ()
{
cpScanViewController *cp;
}
#end
#implementation cpViewController
- (void)viewDidLoad
{
// set delegate
cp = [[cpScanViewController alloc] init];
[cp setDelegate:self];
}
-(void)receivedUPC:(NSString *)Scan{
// Nothing happening
NSLog(#"%#", Scan);
NSLog(#"The %#", cp.UPC);
}
#end
Because you are not calling receivedUPC method from your cpScanViewController class. You need to add [_delegate receivedUPC:self], some where in order to invoke this delegat method in cpViewController class.
I was able to get it working by using the method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
cpScanViewController *vd = (cpScanViewController *)[segue destinationViewController];
vd.delegate = self;
}
Because I made the connection in storyboards I didn't realize it needed to have the method.
I can't see the webview when I launch tha app..
In the delegate that's my code
#import <Cocoa/Cocoa.h>
#include "NewsViewController.h"
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (nonatomic,strong) IBOutlet NSSplitView *splitView;
#property (nonatomic,strong) IBOutlet NewsViewController *newsViewController;
#end
#import "AppDelegate.h"
#implementation AppDelegate
- (void)dealloc
{
[super dealloc];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
// 1. Create the master View Controller
self.newsViewController = [[NewsViewController alloc] initWithNibName:#"NewsViewController" bundle:nil];
// 2. Add the view controller to the Window's content view
[self.splitView addSubview:self.newsViewController.view];
}
#end
That's my viewcontroller
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface NewsViewController : NSViewController{
IBOutlet WebView *web;
}
#property (assign) IBOutlet WebView *web;
#end
#import "NewsViewController.h"
#interface NewsViewController ()
#end
#implementation NewsViewController
#synthesize web;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
}
return self;
}
-(void)loadView{
NSString *urlAddress = #"http://www.google.com/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[[web mainFrame] loadRequest:requestObj];
}
#end
if I out one label on my view in the controller everything is fine but if I put a webview I see only a grey window.
Why this?
thanks
I do believe you need to call [super loadView] in your loadView implementation.
Question with trying to simply clear a mutablearray in another class in objective C
the code all runs fine, just the data doesnt get wiped....
SecondViewController.h
#interface SecondViewController : UIViewController <UITableViewDelegate,UITextFieldDelegate,ABPeoplePickerNavigationControllerDelegate>
{
AppDelegate *appDelegate;
}
#property (nonatomic, retain) NSMutableArray *contactsArray;
Thats the declaration of the mutablearray...
then from the class that i am trying to delete it.
listViewController.h
#import "SecondViewController.h"
#interface ListViewController : UIViewController <UITableViewDelegate,ABPeoplePickerNavigationControllerDelegate,UITabBarControllerDelegate>{
AppDelegate *appDelegate;
SecondViewController *secondViewControl;
}
#property (nonatomic, retain) IBOutlet UITableView *table;
then in the main file i use the code
[secondViewControl.contactsArray removeAllObjects];
but then when i check the array it has not been emptied...
EDIT:
#implementation ListViewController
#synthesize table;
/*- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}*/
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
secondViewControl = [[SecondViewController alloc]init];
I have two error :No visible #interface for 'UIWebView' declares the selector 'highlightAllOccurencesOfString:'
another one:No visible #interface for 'UIWebView' declares the selector 'removeAllHighlights'
Please someone help me.
WBSecondViewController.h
#import <UIKit/UIKit.h>
#interface WBSecondViewController : UIViewController <UIWebViewDelegate, UIScrollViewDelegate>{
}
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#property(copy) NSArray *menuItems;
#property (weak, nonatomic) IBOutlet UIToolbar *webToolBar;
- (IBAction)back:(id)sender;
- (IBAction)foward:(id)sender;
-(IBAction)searchButtonPressed:(id)sender;
-(IBAction)clearHighlights:(id)sender;
#end
WBSecondViewController.m
#import "WBSecondViewController.h"
#import "Word.h"
#import "WordController.h"
#import "AddWordController.h"
#import "WBAppDelegate.h"
#import "WBFirstViewController.h"
#import "SearchWebView.h"
#interface WBSecondViewController ()
#end
#implementation WBSecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Second", #"Second");
self.tabBarItem.image = [UIImage imageNamed:#"second"];
}
return self;
}
- (void)viewDidLoad
{
UIMenuController *menu = [UIMenuController sharedMenuController];
[super viewDidLoad];
NSURL *theURL = [NSURL URLWithString:#"http://www.google.co.jp"];
[_webView loadRequest:[NSURLRequest requestWithURL:theURL]];
}
-(IBAction)searchButtonPressed:(id)sender{
[_webView highlightAllOccurencesOfString:#"cat"];
}
-(IBAction)clearHighlights:(id)sender{
[_webView removeAllHighlights];
}
SearchWebView.h
#import <Foundation/Foundation.h>
#interface SearchWebView : UIWebView
- (NSInteger)highlightAllOccurencesOfString:(NSString*)str;
- (void)removeAllHighlights;
#end
SearchWebView.m
#import "SearchWebView.h"
#implementation SearchWebView
- (NSInteger)highlightAllOccurencesOfString:(NSString*)str
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"UIWebViewSearch" ofType:#"js"];
NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[self stringByEvaluatingJavaScriptFromString:jsCode];
NSString *startSearch = [NSString stringWithFormat:#"uiWebview_HighlightAllOccurencesOfString('%#')",str];
[self stringByEvaluatingJavaScriptFromString:startSearch];
NSString *result = [self stringByEvaluatingJavaScriptFromString:#"uiWebview_SearchResultCount"];
return [result integerValue];
}
- (void)removeAllHighlights
{
[self stringByEvaluatingJavaScriptFromString:#"uiWebview_RemoveAllHighlights()"];
}
#end
U have subclassed uiwebview and called it SearchWebView but then when you create an instance of web view in your wbsecondviewcontroller, you use a regular web view instead of the subclass that you created and the regular web view does not have the two extra methods that you defined for that custom one. Above #interface in the wbsecondviewcontroller.h do #class SearchWebView. Then where you declare the property UiWebView, declare it as a SearchWebView instead. In the .m file of the wbsecondviewcontroller do #import "SearchWebView.h"
I keep getting this message (in the title). Just take a quick look at my code if you want to see what I'm doing. I've just started implementing the Peer Picker, so I'm not completely done yet. I just need some advice/help in the first part. The error shows up in the .m file between the two #import statements, which means it has to be some wrong way that I've used the GKPeerPickerController in the header file.
Bluetooth_Ad_Hoc_NetworkAppDelegate.h
#import <UIKit/UIKit.h>
#class Bluetooth_Ad_Hoc_NetworkViewController;
#interface Bluetooth_Ad_Hoc_NetworkAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
Bluetooth_Ad_Hoc_NetworkViewController *viewController;
GKPeerPickerController *picker;
GKSession *session;
IBOutlet UILabel *status;
NSData *data;
}
#property(nonatomic, retain)IBOutlet UILabel *status;
#property(nonatomic, retain)GKPeerPickerController *picker;
#property(nonatomic, retain)GKSession *session;
#property(nonatomic, retain)IBOutlet UIWindow *window;
#property(nonatomic, retain)IBOutlet Bluetooth_Ad_Hoc_NetworkViewController *viewController;
#end
Bluetooth_Ad_Hoc_NetworkAppDelegate.m
#import "Bluetooth_Ad_Hoc_NetworkAppDelegate.h"
#import "Bluetooth_Ad_Hoc_NetworkViewController.h"
#implementation Bluetooth_Ad_Hoc_NetworkAppDelegate
#synthesize status;
#synthesize picker;
#synthesize session;
#synthesize window;
#synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
// allocate and initialize data
data = [[NSData alloc] initWithBytes:&status length:sizeof(status)];
// Allocate and setup peer picker controller
picker = [[GKPeerPickerController alloc] init];
picker.delegate = self;
picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
[picker show];
}
- (void)dealloc {
[status release];
[viewController release];
[window release];
[super dealloc];
}
#end
Have you included this statement in the header file?
#import <GameKit/GameKit.h>
Also you need to include the GameKit framework.