How to use Protocols objective-c - iphone

I need to inherit Picker selected values into the other place .I am trying the below code but null value is coming ..please check where I am going wrong.
I have to Inherit String values that is been passes in the PickerView..please check the code
Picker1.h
#import <UIKit/UIKit.h>
#protocol pickerDelegate <NSObject>
-(void)didFinishPicking:(NSString *)pickedStr;
#end
#interface
#property(nonatomic,retain)id<pickerDelegate>delegate;
picker.m
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
string=[NSString stringWithFormat:#"%#",[list objectAtIndex:row]];
label.text=string;
[self.delegate didFinishPicking:string];
}
- (void)viewDidLoad
{
[super viewDidLoad];
list =[[NSMutableArray alloc]init];
[list addObject:#"a"];
[list addObject:#"b"];
}
Acitivity_1.h
#import <UIKit/UIKit.h>
#import "Picker1.h"
#interface Activity_1 : UIViewController<UIApplicationDelegate, pickerDelegate>{
#property(nonatomic,retain)Picker1 *delegate1;
#property (nonatomic,retain)NSString *str;
#end
Activity_1.m
- (void)viewDidLoad
{
[super viewDidLoad];
**this is how i print the value but value is null**
NSLog(#"delegate1%#",self.delegate1.string);
delegate1 = [[Picker1 alloc] init];
[delegate1 setDelegate : self];
}
-(void)didFinishPicking:(NSString *)pickedStr
{
[self setStr:pickedStr];
}

You are printing out a value of a delegate just before you are setting it up....so it will print null. You should print out your string when the didFinishPicking method is called instead since this is where you are setting up your string.
-(void)didFinishPicking:(NSString *)pickedStr
{
[self setStr:pickedStr];
// print the string you have just picked here if you want
NSLog(#"Picked string: %#",pickedStr);
}
Note one the side: avoid any name convention with number such as Activity_1, Picker1 this is extremely bad code practice.

You are NSLogging delegate before creating self.delegate1 itself
Please use the below lines of code.
delegate1 = [[Picker1 alloc] init];
[delegate1 setDelegate : self];
And put NSLog inside "didFinishPicking"
-(void)didFinishPicking:(NSString *)pickedStr
{
NSLog(#"pickedStr%#", pickedStr);
[self setStr:pickedStr];
}

Related

Correct way to create/use a Singleton NSMutableArray for Xcode 4

I've reviewed (and tried) a bunch of the threads here regarding Singletons and NSMutableArrays. I'm new to Objective-C so please bear with me.
I simply want to create a few arrays that can be accessed from any view/.m file.
What is the best (or most concise) coding for a Singleton?
Below is what I have now and I get
1 warning at .m '#implementation' - "Incomplete implementation"
1 error at usage in a view .m file - "initializer element is not a compile-time constant"
This is the code I have now - my GlobalData.h file:
#import <Foundation/Foundation.h>
#interface GlobalData : NSObject {
NSMutableArray *listOfHeadings;
NSMutableArray *listOfItems1;
NSMutableArray *listOfItems2;
}
#property(nonatomic,retain)NSMutableArray *listOfHeadings;
#property(nonatomic,retain)NSMutableArray *listOfItems1;
#property(nonatomic,retain)NSMutableArray *listOfItems2;
+(GlobalData*)getInstance;
#end
My GlobalData.m file:
#import "GlobalData.h"
#implementation GlobalData
#synthesize listOfHeadings;
#synthesize listOfItems1;
#synthesize listOfItems2;
static GlobalData *instance=nil;
+(GlobalData *)getInstance
{
#synchronized(self)
{
if(instance==nil)
{
instance= [GlobalData new];
}
}
return instance;
}
#end
And in a view .m file (simplified):
#import GlobalData.h
GlobalData *globDat=[GlobalData getInstance]; //error occurs here
Can someone point out the trouble and if there's better coding, please enlighten me - thanks!
EDIT
Here's a few links I've tried to use:
Can i have a single NSMutableArray in my multiple views application?
iPhone help with singleton class
In this case, you might be doing more than you have to. Granted this certainly isn't always the best solution - but you can put your NSMutableArray as a property in your App Delegate and then easily refer to it from any view. By doing it this way - you aren't locking it in as a 'singleton' but there is a 'singleton instance' of it (this helps a great deal for testability).
I have simplified this process here:
YourAppDelegate.h
#property (nonatomic,retain) NSMutableArray *myArray;
YourAppDelegate.m
#synthesize myArray;
YourViewController.m
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
NSMutableArray *myArrayFromAppDelegate = appDelegate.myArray;
From this point - you can do any manipulation on this value.
Here's the "modern" version of a single method to turn any class into a Singleton (in this case formatted as a code snippet). It works in iOS4.x or higher:
+(<#SingletonClassName#> *) sharedInstance
{
static <#SingletonClassName#> *_sharedClient = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedClient = [[self alloc] init];
});
return _sharedClient;
}
But, do you really need a singleton of a single NSMutableArray? You could use the built-on singleton - your application delegate, which is got to by calling:
MyAppDelegate * appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.myMutableArray addObject:...];
The error initializer element is not a compile-time constant is not related to how you create your singleton. The error is how you are accessing your singleton. You are doing this outside of a function:
GlobalData *globDat=[GlobalData getInstance];
This means that you are trying to initialize a global variable (globDat) as the value of the expression [GlobalData getInstance]. You can only initialize global variables to expressions that are "compile-time constants". That means things like 0 or "fred" or 8/2. The value of [GlobalData getInstance] cannot be computed at compile-time, so it cannot be used to initialize the global variable.
Instead, you need to just use [GlobalData getInstance] inside your function bodies wherever you are currently trying to use the globDat variable.
As for the warning, Incomplete implementation, I don't see what's missing. Perhaps you didn't post all of the code from GlobalData.h. Anyway, you should be able to click the warning (where it appears on the right side of the editor window) and have Xcode show you what's missing.
This is the way I create my Singleton:
Singleton.h
#import <Foundation/Foundation.h>
#interface Singleton : NSObject {
NSMutableArray *firstMutableArray;
NSMutableArray *secondMutableArray;
}
#property (nonatomic, retain) NSMutableArray *firstMutableArray;
#property (nonatomic, retain) NSMutableArray *secondMutableArray;
+ (id)sharedSingleton;
#end
Sigleton.m
#import "Singleton.h"
static Singleton *sharedMySingleton = nil;
#implementation Singleton
#synthesize firstMutableArray;
#synthesize secondMutableArray;
#pragma mark Singleton Methods
+ (id)sharedSingleton {
#synchronized(self) {
if (sharedMySingleton == nil) {
sharedMySingleton = [[super allocWithZone:NULL] init];
}
return sharedMySingleton;
}
+ (id)allocWithZone:(NSZone *)zone {
return [[self sharedSingleton] retain];
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (unsigned)retainCount {
return UINT_MAX;
}
- (oneway void)release {
// Never release
}
- (id)autorelease {
return self;
}
- (id)init {
if (self = [super init]) {
firstMutableArray = [[NSMutableArray alloc] initWithObjects:nil];
secondMutableArray = [[NSMutableArray alloc] initWithObjects:nil];
}
return self;
}
- (void)dealloc {
[firstMutableArray release];
[secondMutableArray release];
[super dealloc];
}
#end
Then, when you want to call your Singleton:
#import "Singleton.h"
Singleton *singleton = [Singleton sharedSingleton];
singleton.firstMutableArray = ...
singleton.secondMutableArray = ...

endless loop adding a subview

I am a little confused, and after countless attempts and read several articles I decided to write.
my problem is that if you call a method from a class (xml) and it is aimed at viewcontroller all goes well
but if I might add [self.view add...] it back to the top reloading the viewDidLoad of the viewController class entering into an endless loop.
this is what I do
class (ViewController)
.h
#import <UIKit/UIKit.h>
#class XMLStuff;
#interface skiSpeedViewController : UIViewController {
}
#property (nonatomic, retain) XMLStuff *xml;
.m
- (void)viewDidLoad
{
[super viewDidLoad];
xml.skiSpeedC = self;
GpsStuff *gps = [GpsStuff alloc];
[gps init];
}
gps.m
- (id)init
{
self = [super init];
if (self) {
xml = [XMLStuff alloc];
}
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[xml lon:newLocation.coordinate.longitude lat:newLocation.coordinate.latitude];
xml.h
#import "skiSpeedViewController.h"
#class skiSpeedViewController;
#interface XMLStuff : NSObject <NSXMLParserDelegate> {
}
#property (retain, nonatomic) skiSpeedViewController *skiSpeedC;
.m
#synthesize skiSpeedC;
- (void) parserDidEndDocument:(NSXMLParser *)parser {
NSLog(#"--%#", self.skiSpeedC); // Return (null)
[self.skiSpeedC riceviDic:datiMeteo];
}
ViewController.m
-(void)riceviDic:(NSMutableDictionary *)dictMeteo {
datiMeteo = [[NSMutableDictionary alloc]initWithDictionary:dictMeteo];
}
}
- (void) parserDidEndDocument:(NSXMLParser *)parser {
classViewController *skiSpeedC = [classViewController alloc];
[skiSpeedC riceviDic:datiMeteo];
}
You are creating a new instance of classViewController every time. Your "xml" class (XMLStuff?) should have a pointer to the view controller and be calling the riceviDic method on that instance.
You're getting an infinite loop because when you allocate the XML object in viewDidLoad, it too starts parsing the XML, then creates more XML objects, which then create more viewControllers...
So, add a property to XMLStuff of type classViewController, and when you create it in viewDidLoad:
xml.skiSpeedC = self;
Then, in parserDidEndDocument:
- (void) parserDidEndDocument:(NSXMLParser *)parser {
[self.skiSpeedC riceviDic:datiMeteo];
}
UPDATE
OK, after your edit things look very different - you seem to have introduced a new class - GpsStuff, which has its own instance of XMLStuff (and a dodgy looking init method which I assume you haven't copied in properly?). Which one is actually parsing your document? XMLStuff in your view controller, or in GPSStufF? I'm guessing the one in GPSStuff, which you haven't set the skiSpeedC property for. I was previously assuming that you were calling everything from your view controller.
Why not remove the creation of a new XMLStuff object from GPSStuff, and when you create GPSStuff in your view controller, pass the xml object you've created into it:
- (void)viewDidLoad
{
[super viewDidLoad];
GpsStuff *gps = [[GpsStuff alloc] init];
XMLStuff *xml = [[XMLStuff alloc] init];
xml.skiSpeedC = self;
gps.xml = xml;
[xml release];
}
Also, the skiSpeedC property should probably not be retain, since it is essentially a delegate assignment and the view controller is not going to be released before you release the xml parser.
As a note, by convention you should be initializing objects like this:
GPSStuff *gps = [[GPSStuff alloc] init];
Not on two lines. You want what is returned from init to be assigned to your variable.

NSArray losing data in objective-c, Could someone explain why?

I have an nsarray that when I NSLog it from one of my methods (inside WorkOutList.m) I can see its contents, but when I try to look inside it from a different method inside WorkOutList.m it seems to be empty. I am aware that my memory management needs some work, could anyone help explain whats happening here?
I am using popViewControllerAnimated:YES to return the view from a tableView back to a view controller, but just before I do that I set my array in a method inside WorkOutList. When I NSLog that array from that same method I am returned results, however when i NSLog it from else where it is returned empty.
I have been told that it might be the viewDidLoad method where the array is init, but that the other array in that method customWorkouts still retains its data. So i dunno, any explanation would be really helpful. I want this to work, but I also really want to understand it so I can get on with coding correctly.
Thanks so much!
WorkOutList.h
#import <UIKit/UIKit.h>
#interface WorkOutList : UIViewController {
NSManagedObjectContext *managedObjectContext;
NSMutableArray *customWorkouts;
NSArray *passedWorkout;
}
#property(nonatomic, retain)NSManagedObjectContext *managedObjectContext;
#property(nonatomic, retain)NSMutableArray *customWorkouts;
#property(nonatomic, retain)NSArray *passedWorkout;
-(IBAction)customWorkouts:(id)sender;
-(void)passWorkoutBack:(NSArray *)workout;
#end
WorkOutList.m
#implementation WorkOutList
#synthesize managedObjectContext, customWorkouts, passedWorkout;
- (void)viewDidLoad {
[self setupContext];
NSLog(#"View Did Load");
customWorkouts = [[NSMutableArray alloc] init];
passedWorkout = [[NSArray alloc] init];
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self fetchWorkoutList];
NSLog(#"View will Appear");
NSLog(#"Array from View Will Appear : %#", passedWorkout);
}
-(IBAction)customWorkouts:(id)sender{
CoCoachAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
SelectedWorkout *selectedWorkout = [[SelectedWorkout alloc] initWithStyle:UITableViewStyleGrouped];
[selectedWorkout recieveNeededData:customWorkouts];
[appDelegate.practiceNavController pushViewController:selectedWorkout animated:YES];
[selectedWorkout release];
}
-(void)passWorkoutBack:(NSArray *)workout{
passedWorkout = workout;
[passedWorkout retain];
}
- (void)dealloc {
[super dealloc];
}
SelectedWorkout.h
#import <UIKit/UIKit.h>
#interface SelectedWorkout : UITableViewController {
NSMutableArray *workoutListForTable;
}
#property(nonatomic,retain)NSMutableArray *workoutListForTable;
-(void)recieveNeededData:(NSMutableArray *)workoutList;
#end
SelectedWorkout.m(aside from all the stuff to set up the tableView)
#implementation SelectedWorkout
#synthesize workoutListForTable;
-(void)recieveNeededData:(NSMutableArray *)workoutList{
if (workoutListForTable != workoutList) {
workoutListForTable = workoutList;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
CoCoachAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
WorkOutList *workoutListView = [[WorkOutList alloc]init];
[workoutListView passWorkoutBack:[workoutListForTable objectAtIndex:indexPath.row]];
[appDelegate.practiceNavController popViewControllerAnimated:YES];
}
- (void)dealloc {
[workoutListForTable release];
[super dealloc];
}
NSLog(#"other table : %#", workoutListForTable);
[workoutListForTable retain];
}
In this line:
passedWorkout = [[NSArray alloc] init];
You're creating an immutable array with nothing in it. What do you want it to contain?
It looks to me that although you are initializing your array with or so I assume, since on the other line you are only allocating an empty array.
[self fetchWorkoutList]
You are resetting it here every time:
[workoutListView passWorkoutBack:[workoutListForTable objectAtIndex:indexPath.row]];
As a note here:
-(void)passWorkoutBack:(NSArray *)workout{
passedWorkout = workout;
[passedWorkout retain];
}
You have the property passedWorkout as retain already, however you need to call it on self
-(void)passWorkoutBack:(NSArray *)workout{
self.passedWorkout = workout;
}

Delegate not working

I have this code in my viewController:
- (GraphModel *)graphModel
{
if (!graphModel) {
graphModel = [[GraphModel alloc] init];
NSLog(#"graphModel = %#", graphModel);
}
return graphModel;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.graphView.delegate = [self graphModel];
NSLog(#"self.graphview.delegate = %#", self.graphView.delegate);
[self updateUI];
}
but the NSLog just says (null) for self.graphview.delegate
even though the NSLog in graphModel says that I successfully created an object. How can this be?
this is the code for the graphViewDelegate
#class GraphView;
#protocol GraphViewDelegate
- (double)yValueForGraphView:(GraphView *)requestor atPosition:(int)i withPrecision:(int)precision;
- (double)scaleForGraphView:(GraphView *)requestor;
#end
#interface GraphView : UIView {
id <GraphViewDelegate> delegate;
}
#property (assign) id <GraphViewDelegate> delegate;
#end
and then I have #synthesize delegate in graphView.m
Most likely guess: graphView is nil. Calling any method on a nil object has no effect and returns nil, and the .delegate is actually a call to the getter or setter as appropriate. I recommend you add:
NSLog(#"self.graphview = %#", self.graphView);
As a quick verification.

Newbie question: NSOperation for iphone SDK

Hi I got some problem with NSOperation .
I always got error at self = [super init]; (already use break point to find this)
it always return "Program received signal: EXC_BAD_ACCESS" all the time
//AddThread.h
#interface AddThread : NSOperation
{
NSString * str;
}
#property (nonatomic,retain) NSString * str;
-(id) initWithString:(NSString *) tmpStr;
#end
and for .m
//AddThread.m
#import "AddThread.h"
#implementation AddThread
#synthesize str;
- (id) initWithString:(NSString *)tmpStr
{
self = [super init];
if (self != nil)
{
self.str = tmpStr;
}
//NSLog(self);
//[super init];
return self;
}
- (void) main
{
NSLog(self.str);
}
- (void) dealloc{
[str release];
str = nil;
[super dealloc];
}
#end
well I stuck with this for while and if possible any resources ,articles things for basic example of NSoperation?
In your main method, you are calling NSLog(self.str) - While this will work if the object you pass in is a string, it won't work if you continue to try and log other objects. NSLog takes a format string as a parameter. If you just do NSLog(self) like you are in some of your commented code, and self is not a string, it'll crash because it expected a string. You should do NSLog(#"self: %#", self) the %# will print out the string returned by an objects description method.
Other than that, your init method looks fine, how exactly are you creating an instance of this object? Could you show the code for that? The problem may lie there.