"Type name requires a specifier or qualifier" error - ios4

I have the following code but can't compile it because I have a "Type name requires a specifier or qualifier" error" for (self).
How to fix this error? I have compared it with the original code and there are no differences, so I don't know what's going on.
#import "CurrentTimeViewController.h"
#implementation CurrentTimeViewController
{
// Call the superclass's designated initializer
self = [super initWithNibName:#"CurrentTimeViewController"
bundle:nil];
if (self) {
// Get the tab bar item
UITabBarItem *tbi = [self tabBarItem];
// Give it a label
[tbi setTitle:#"Time"];
}
return self;
}
Here is the code from the mirror file HynososViewController.h, and which I cut, pasted and modified:
#import "HypnososViewController.h"
#implementation HypnososViewController
- (id) init
{
// Call the superclass's designated initializer
self = [super initWithNibName:nil
bundle:nil];
if (self) {
// Get the tab bar item
UITabBarItem *tbi = [self tabBarItem];
// Give it a label
[tbi setTitle:#"Hypnosis"];
}
return self;
}
- (id) initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
{
// Disregard parameters - nib name is an implementation detail
return [self init];
}
// This method gets called automatically when the view is created
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"Loaded the view for HypnosisViewController");
// Set the background color of the view so we can see it
[[self view] setBackgroundColor:[UIColor orangeColor]];
}
#end
Here is the complete code for CurrentTimeViewController.h:
#import "CurrentTimeViewController.h"
#implementation CurrentTimeViewController
{
// Call the superclass's designated initializer
self = [super initWithNibName:#"CurrentTimeViewController"
bundle:nil];
if (self) {
// Get the tab bar item
UITabBarItem *tbi = [self tabBarItem];
// Give it a label
[tbi setTitle:#"Time"];
}
return self;
}
- (id) initWithNibName:(NSString *)nibName bundle:(NSBundle *)Bundle
{
// Disregard parameters - nib name is an implementation detail
return [self init];
}
// This method gets called automatically when the view is created
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"Loaded the view for CurrentTimeViewController");
// Set the background color of the view so we can see it
[[self view] setBackgroundColor:[UIColor greenColor]];
}
#end

Is the code above a cut-and-paste of EXACTLY what you are trying to compile? If so, I think you are missing something very important that would make that code block a method implementation:
-(id)init // This, or something like it, is missing???
{
...
}
Check your code, here, and in your project. :-)

Related

Using GCD to Parse KML With Apple's KMLViewer

I'm using Apple's KMLViewer to load a KML file and display it in a MapView. There are over 50,000 lines of coordinates in the KML file which, of course, causes it to load slowly. In an attempt to speed things up, I'm trying to perform the parsing in another thread using GCD.
I have it working reasonably well as far as it is displaying properly and the speed is acceptable. However, I'm getting intermittent runtime errors when loading the map. I suspect it is because the way I have things laid out, the UI is being updated within the GCD block. Everything I'm reading says the UI should be updated in the main thread or else runtime errors can occur which are intermittent and hard to track down. Well, that's what I'm seeing.
The problem is, I can't figure out how to update the UI in the main thread. I'm still new to iOS programming so I'm just throwing things against the wall to see what works. Here is my code, which is basically Apple's KMLViewerViewController.m with some modifications:
#import "KMLViewerViewController.h"
#implementation KMLViewerViewController
- (void)viewDidLoad
{
[super viewDidLoad];
activityIndicator.hidden = TRUE;
dispatch_queue_t myQueue = dispatch_queue_create("My Queue",NULL);
dispatch_async(myQueue, ^{
// Locate the path to the route.kml file in the application's bundle
// and parse it with the KMLParser.
NSString *path = [[NSBundle mainBundle] pathForResource:#"BigMap" ofType:#"kml"];
NSURL *url = [NSURL fileURLWithPath:path];
kmlParser = [[KMLParser alloc] initWithURL:url];
[kmlParser parseKML];
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI
// Add all of the MKOverlay objects parsed from the KML file to the map.
NSArray *overlays = [kmlParser overlays];
[map addOverlays:overlays];
// Add all of the MKAnnotation objects parsed from the KML file to the map.
NSArray *annotations = [kmlParser points];
[map addAnnotations:annotations];
// Walk the list of overlays and annotations and create a MKMapRect that
// bounds all of them and store it into flyTo.
MKMapRect flyTo = MKMapRectNull;
for (id <MKOverlay> overlay in overlays) {
if (MKMapRectIsNull(flyTo)) {
flyTo = [overlay boundingMapRect];
} else {
flyTo = MKMapRectUnion(flyTo, [overlay boundingMapRect]);
}
}
for (id <MKAnnotation> annotation in annotations) {
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
if (MKMapRectIsNull(flyTo)) {
flyTo = pointRect;
} else {
flyTo = MKMapRectUnion(flyTo, pointRect);
}
}
// Position the map so that all overlays and annotations are visible on screen.
map.visibleMapRect = flyTo;
});
});
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
activityIndicator.hidden = FALSE;
[activityIndicator startAnimating];
}
#pragma mark MKMapViewDelegate
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
return [kmlParser viewForOverlay:overlay];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
return [kmlParser viewForAnnotation:annotation];
}
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
{
[activityIndicator stopAnimating];
activityIndicator.hidden = TRUE;
}
#end
Suggestions?

How can I remove an NSPersistentStore from my coordinator once it is unused?

I'm using in-memory NSPersistentStores to hold transient objects. Once the store is unused - which doesn't necessarily mean empty - I'd like to remove it from the coordinator to free up the memory it uses.
My first attempt was to have each controller create a store in its init, and remove it in its dealloc. This didn't work, because background threads were still using NSManagedObjects in that store; it was being removed while it was still being used, and things broke.
My second attempt was to wrap the stores in an object that could remove the real store once the wrapped object's retain count hit zero. That way, the background threads could retain the wrapped store, and it would only be removed once nothing was using it any more. I used message forwarding to create a proxy object, like so:
#interface MyStoreWrapper : NSObject
#property (nonatomic, retain) NSPersistentStore *persistentStore;
+(MyStoreWrapper *) wrappedInMemoryStore;
+(MyStoreWrapper *) wrapStore:(NSPersistentStore *)aStore;
-(id) initWithStore:(NSPersistentStore *)aStore;
#end
#implementation MyStoreWrapper
#synthesize persistentStore;
+(MyStoreWrapper *)wrappedInMemoryStore
{
NSError *error = nil;
NSPersistentStore *store = [[[MyAppDelegate sharedDelegate] persistentStoreCoordinator] addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:&error];
if (error)
{
[NSException raise:#"Core data error" format:#"Could not add temporary store: %#, %#", error, [error userInfo]];
}
return [self wrapStore:store];
}
+(MyStoreWrapper *)wrapStore:(NSPersistentStore *)aStore
{
return [[[self alloc] initWithStore:aStore] autorelease];
}
-(id)initWithStore:(NSPersistentStore *)aStore
{
self = [super init];
if (self)
{
self.persistentStore = aStore;
}
return self;
}
-(void)dealloc
{
NSError *error = nil;
[[[MyAppDelegate sharedDelegate] persistentStoreCoordinator] removePersistentStore:self.persistentStore error:&error];
if (error)
{
[NSException raise:#"Core data error" format:#"Could not remove temporary store: %#, %#", error, [error userInfo]];
}
[super dealloc];
}
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
if ([self.persistentStore respondsToSelector:[anInvocation selector]])
{
[anInvocation invokeWithTarget:self.persistentStore];
}
else
{
[super forwardInvocation:anInvocation];
}
}
- (BOOL)respondsToSelector:(SEL)aSelector
{
if ([super respondsToSelector:aSelector])
{
return YES;
}
else
{
return [self.persistentStore respondsToSelector:aSelector];
}
}
- (NSMethodSignature*)methodSignatureForSelector:(SEL)aSelector
{
NSMethodSignature* signature = [super methodSignatureForSelector:aSelector];
if (!signature)
{
signature = [self.persistentStore methodSignatureForSelector:aSelector];
}
return signature;
}
+(BOOL)instancesRespondToSelector:(SEL)aSelector
{
if ([super instancesRespondToSelector:aSelector])
{
return YES;
}
else
{
return [NSPersistentStore instancesRespondToSelector:aSelector];
}
}
+(NSMethodSignature *)instanceMethodSignatureForSelector:(SEL)aSelector
{
NSMethodSignature* signature = [super instanceMethodSignatureForSelector:aSelector];
if (!signature)
{
signature = [NSPersistentStore instanceMethodSignatureForSelector:aSelector];
}
return signature;
}
#end
...but this wrapped object doesn't seem to be a valid substitute in e.g. NSFetchRequests.
Have I made some mistake in my wrapper class? Is there some other way to remove an NSPersistentStore from my coordinator once it is unused?
You can't have a question that says both Once the store is unused and then because background threads were still using NSManagedObjects in that store.
By definition, if background threads are still using it, it's not unused ;)
Your method is correct but you are deallocing too early. Your background threads should be retaining the store if they are interested in it's contents. That way, as soon as all your background threads are finished, they will call release and the store will dealloc itself safely.

Having Placemark/Annotation in the center of your mapView, even if you scroll

I've spent many hours trying to figure how to do this:
Having a placemark/annotation in the centerCoordinate of your mapView, when you scroll the map, the placemark should always stays in the center.
I've seen another app doing this too!
Found my question in How to add annotation on center of map view in iPhone?
There's the answer :
If you want to use an actual annotation instead of just a regular view positioned above the center of the map view, you can:
use an annotation class with a settable coordinate property (pre-defined MKPointAnnotation class eg). This avoids having to remove and add the annotation when the center changes.
create the annotation in viewDidLoad
keep a reference to it in a property, say centerAnnotation
update its coordinate (and title, etc) in the map view's regionDidChangeAnimated delegate method (make sure map view's delegate property is set)
Example:
#interface SomeViewController : UIViewController <MKMapViewDelegate> {
MKPointAnnotation *centerAnnotation;
}
#property (nonatomic, retain) MKPointAnnotation *centerAnnotation;
#end
#implementation SomeViewController
#synthesize centerAnnotation;
- (void)viewDidLoad {
[super viewDidLoad];
MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
pa.coordinate = mapView.centerCoordinate;
pa.title = #"Map Center";
pa.subtitle = [NSString stringWithFormat:#"%f, %f", pa.coordinate.latitude, pa.coordinate.longitude];
[mapView addAnnotation:pa];
self.centerAnnotation = pa;
[pa release];
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
centerAnnotation.coordinate = mapView.centerCoordinate;
centerAnnotation.subtitle = [NSString stringWithFormat:#"%f, %f", centerAnnotation.coordinate.latitude, centerAnnotation.coordinate.longitude];
}
- (void)dealloc {
[centerAnnotation release];
[super dealloc];
}
#end
Now this will move the annotation but not smoothly. If you need the annotation to move more smoothly, you can add a UIPanGestureRecognizer and UIPinchGestureRecognizer to the map view and also update the annotation in the gesture handler:
// (Also add UIGestureRecognizerDelegate to the interface.)
// In viewDidLoad:
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
panGesture.delegate = self;
[mapView addGestureRecognizer:panGesture];
[panGesture release];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
pinchGesture.delegate = self;
[mapView addGestureRecognizer:pinchGesture];
[pinchGesture release];
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
centerAnnotation.coordinate = mapView.centerCoordinate;
centerAnnotation.subtitle = [NSString stringWithFormat:#"%f, %f", centerAnnotation.coordinate.latitude, centerAnnotation.coordinate.longitude];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
//let the map view's and our gesture recognizers work at the same time...
return YES;
}

How to switch between UITabBarController programmatically

I have an application with UITabBarController, where the first tab contains a ViewController of an HomePage. What I have to do, is to switch between the tabs (this is pretty simple: [[self tabBarController] setSelectedIndex:index]), AND to navigate through outlets of the selectedTab from the "HomePage".
Just to explain myself: TabElement1--->TabElementX---->UISegmentedController:segmentY
The problem is that the UISegmentedController is nil because it is not initialized yet (at least the first time I do the operation). How should I fix this problem? The tab elements are loaded with nibs.
EDIT-- Here's some code:
#implementation HomeViewController // Tab Indexed 0
// ...
- (void)playVideoPreview {
NSArray *array;
array = [[self tabBarController] viewControllers];
// This is a test where I programmatically select the tab AND the switch.
[[[array objectAtIndex:2] switches] setSelectedSegmentIndex:1];
[[self tabBarController] setViewControllers:array];
}
#end
#implementation TGWebViewController // Tab Indexed 2
// ...
#synthesize switches; // In .h file: #property (nonatomic, retain) IBOutlet UISegmentedControl switches; Properly linked within the XIB.
- (IBAction)switchHasChangedValue {
// Foo operations.
}
Now the first time I fire playVideoPreview I manage to get Into the Tab Indexed 2, TGWebViewController, but switches doesn't exists yet, so I find myself with the segmentedControl named "switches" with the first segment selected. If I get back to HomeViewController, then I fire again playVideoPreview, I get the correct behaviour.
I've fixed the problem using the delegates and a boolean. Now, when the loading of the ViewController at index 2 of TabBar is finished, it sends a message to its delegate which tells what segment has to be selected.
EDIT Here's the code (hope it helps):
// Method in the first View that asks to tab the tab bar to launch the other
// view controller
- (void)playVideoPreview {
NSArray *array;
array = [[self tabBarController] viewControllers];
if ( ![[array objectAtIndex:2] catSwitch] ) {
[[array objectAtIndex:2] setDelegate:self];
[[array objectAtIndex:2] setHasBeenLaunchedByDelegate:YES];
} else {
[self selectTab];
}
[[self tabBarController] setViewControllers:array];
[[self tabBarController] setSelectedIndex:2];
}
// Now the operations performed by the second View Controller
- (void)somewhereInYourCode {
if ( hasBeenLaunchedByDelegate ) {
[[self delegate] selectTab];
}
}
// In the First View Controller this is the delegate method,
// launched from the Second View Controller
- (void)selectTab {
NSArray *array;
array = [[self tabBarController] viewControllers];
[[[array objectAtIndex:2] catSwitch] setSelectedSegmentIndex:[[bannerPreview pageControl] currentPage]];
}
// Some declaration
#protocol SecondViewControllerDelegate;
class SecondViewController : ViewController {
id<TGWebViewControllerDelegate> delegate;
}
#end
#protocol SecondViewControllerDelegate
- (void)selectTab;
#end
// Meanwhile in the first view
class FirstViewController : ViewController <SecondViewControllerDelegate> {
// ...
}

iphone gmail code examples

I want to display login window and then type login and password credencial it will open the account. So i tried but it shows error like
/81: error: 'didReceiveMemoryWarning' undeclared (first use in this function)
*:81: error: expected ';' before '{' token*
100: error: expected declaration or statement at end of input
and i posted below the code please help
Thanks in advance
.h file
#import <UIKit/UIKit.h>
#interface Password1ViewController : UIViewController {
UITextField *textfieldName;
UITextField *textfieldPassword;
}
#property (nonatomic, retain) IBOutlet UITextField *textfieldName;
#property (nonatomic, retain) IBOutlet UITextField *textfieldPassword;
#end
.m file
#import "Password1ViewController.h"
#implementation Password1ViewController
#synthesize textfieldName;
#synthesize textfieldPassword;
-(void)alertView:(UIAlertView *)alertview clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != [alertview cancelButtonIndex])
{
NSLog(#"Name: %#", textfieldName.text);
NSlog(#"Name: %#", textfieldPassword.text);
}
}
-(void) someMethod
{
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:#"Please Login" message:#"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Submit",nil];
[alert addTextFieldWithValue:#"" label:#"User Name"];
[alert addTextFieldWithValue:#"" label:#"Password"];
textfieldName = [alert textFieldAtIndex:0];
textfieldName.keyboardType = UIKeyboardTypeAlphabet;
textfieldName.keyboardAppearance = UIKeyboardAppearanceAlert;
textfieldName.autocorrectionType = UITextAutocorrectionTypeNo;
textfieldPassword = [alert textFieldAtIndex:1];
textfieldPassword.clearButtonMode = UITextFieldViewModeWhileEditing;
textfieldPassword.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
textfieldPassword.keyboardAppearance = UIKeyboardAppearanceAlert;
textfieldPassword.autocorrectionType = UITextAutocorrectionTypeNo;
textfieldPassword.secureTextEntry = YES;
[alert show];
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[textfieldName release];
[textfieldPassword release];
[super dealloc];
}
#end
You were missing a closing brace which I corrected in my edit. So, try now with the edited code.

Resources