problem with singleton and KVO - iphone

In my app , i created my custom class and i am observing one of its property using KVO ,so that if its value changed it instantly display in firstview controller object (label or .. )
sample code
myCustomClass.h
#interface myCustomClass : NSObject {
NSString * text;
}
#property (nonatomic, retain) NSString * text;
- (void)changetext;
myCustomClass.m
#implementation myCustomClass
#synthesize text;
static myCustomClass * _sharedInstance;
- (id)init
{
if ((self = [super init])) {
text = # "";
}
return self;
}
+ (myCustomClass *)sharedInstance
{
if (!_sharedInstance) {
_sharedInstance = [[myCustomClass alloc] init];
}
return _sharedInstance;
}
- (void)changetext {
text = # "changed";
}
firstViewController.h
#interface FirstViewController:UIViewController {
IBOutlet UILabel * label;
}
#property (nonatomic, retain) IBOutlet UILavel * label;
firstviewController.m
#implementation FirstViewController
#synthesize label;
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id) object change:(NSDictionary *)change context:(void *)context
{
label.text = [change valueForKey:# "new"];
}
- (void)viewDidLoad {
myCustomClass * myEngine = [myCustomClass sharedInstance];
[myEngine addObserver : self forKeyPath : # "text" options : NSKeyValueObservingOptionNew context : nil];
[myEngine changetext];
[super viewDidLoad];
}
but its not changing the data , can any one please tell me where i am wrong ?
thanks in advance
P.S : i wrote in hurry forgive me if any mistakes in writing and sorry for my bad english .

When you assign to an instance variable directly rather than going through a setter, you need to issue change notifications yourself with willChangeValueForKey: and didChangeValueForKey:. There's no magic to variable assignment.

Replace text with self.text as The observer sub-system is tied into the getter and setter methods synthesized as the property.

Related

How can I create a wrapper to use blocks for a class that uses callbacks?

I'm diving into iOS programming and I'm learning how to use blocks. I have a sucky, over-engineered library that I'm using in my project and it uses a single callback method to handle all data requests...
#protocol SuckyClassDelegate <NSObject>
-(void)returnedSuckyData:(NSMutableDictionary*)data;
#end
#interface SuckyClass: NSObject
#property (nonatomic, weak) id<SuckyClassDelegate> delegate;
-(void)getSuckyData;
#end
#interface MyViewController: UIViewController <SuckyClassDelegate>
-(void)requestDataFromSuckyClass;
#end
I'd like to create a wrapper class for the SuckyClass that allows me to use blocks when I need to access data from the SuckyClass, but I don't know how to do this. I'd like to have something like this...
#interface SuckyClassWrapper
- (void)requestDataWithSuccessBlock:(void(^)((NSMutableDictionary*)data))successBlock;
#end
#implementation MyViewController
-(void)requestDataFromSuckyClass {
SuckyClassWrapper *wrapper = [[SuckyClassWrapper alloc] init];
[wrapper requestDataWithSuccessBlock:^(NSMutableDictionary *data) {
NSLog(#"%#", data);
}
}
#end
...but I can't figure out how to convert the callback process into blocks. Can anyhow give me some direction here?
Thanks in advance for your wisdom!
By the way, I just whipped up the code without testing it, so I apologize if there are any typos.
The trick is to copy the completion block to a class iVar that you can then call later.
#property (nonatomic, copy) void (^errorHandler)(NSError *);
#property (nonatomic, copy) void (^successHandler)(NSString *);
Here is a method that saves two blocks for use later and then calls another class method:
- (void)methodWithErrorHandler:(void(^)(NSError *error))errorBlock successHandler: (void(^)(NSString *data))successBlock
{
// Copy the blocks to use later
self.successHandler = successBlock;
self.errorHandler = errorBlock;
// Run code
[self doOtherThings];
}
Later - when what we want to do has completed, we have another method that we call to run the blocks. In this silly example code we check to see if a class property self.error is nil. If it is not nil, we send that error to our saved error block. If it is nil, we pass self.data to the success block.
- (void)finishThingsUp
{
// Check to see if we should call the error block or the success block
if (self.error) {
self.errorHandler(self.error);
} else {
self.successHandler(self.data);
}
// Clean up the blocks
self.errorHandler = nil;
self.successHandler = nil;
}
We could use like this:
typedef void (^SuccessDataBlock)(NSMutableDictionary *);
#interface SuckyClassWrapper : NSObject <SuckyClassDelegate>
#property (nonatomic, retain) NSData *inputData;
#property (nonatomic, copy) SuccessDataBlock completionHandler;
+ (id)requestData:(NSData *)data successBlock:(SuccessDataBlock)handler;
#end
#implementation SuckyClassWrapper
#synthesize inputData;
#synthesize completionHandler;
- (id)initWithData:(NSData *)data completionHandler:(SuccessDataBlock)handler
{
self = [super init];
if (self != nil)
{
inputData = [data retain];
self.completionHandler = handler;
}
return self;
}
+ (id)requestData:(NSData *)data successBlock:(SuccessDataBlock)handler
{
return [[[self alloc] initWithData:data completionHandler:handler] autorelease];
}
//implement SuckyClass delegate
- (void)returnedSuckyData:(NSMutableDictionary *)data
{
self.completionHandler(data);
}
#end
Usage:
SuckyClassWrapper *wrapper = [SuckyClassWrapper requestData:data successBlock:^(NSMutableDictionary *successData) {
//your code here
}];

Passing Values Between Master and Detail in UISplitViewController Using Storyboards

I have defined the protocol in Customer.h file which is shown below:
#class Customer;
#protocol CustomerDelegate <NSObject>
-(void) didSelectCustomer:(Customer *) customer;
#end
#interface Customer : NSObject
{
}
#property (nonatomic,copy) NSString *name;
#property (nonatomic,copy) NSString *occupation;
#end
The MasterViewController (left side) invokes the didSelectCustomer method as shown below:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Customer *selectedCustomer = [customers objectAtIndex:[indexPath row]];
[self.delegate didSelectCustomer:selectedCustomer];
}
Now, I need to tell the DetailViewController (right side) to do something. The DetailViewController complies with the CustomerDelegate protocol.
#interface DetailViewController : UIViewController<UISplitViewControllerDelegate,CustomerDelegate>
{
}
-(void) didSelectCustomer:(Customer *)customer
{
NSLog(#"sssdasdasdasd");
}
The didSelectCustomer method is never invoked. I think I need to set the masterViewController.delegate = self but I am not sure where to set this thing up.
UPDATE 1:
I added the instance of MasterViewController inside the DetailViewController but it did not work:
- (void)viewDidLoad
{
[super viewDidLoad];
MasterViewController *master = [[MasterViewController alloc] init];
master.delegate = self;
}
SOLUTION:
In AppDelegate:
else
{
UISplitViewController *splitViewController = (UISplitViewController *) self.window.rootViewController;
splitViewController.delegate = [splitViewController.viewControllers lastObject];
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
// splitViewController.delegate = (id)navigationController.topViewController;
DetailViewController *detail =(DetailViewController *) [splitViewController.viewControllers lastObject];
UINavigationController *masterNavigationController = [splitViewController.viewControllers objectAtIndex:0];
MasterViewController *master = (MasterViewController *)masterNavigationController.topViewController;
master.delegate = detail;
}
You never explicitly declare yourself as the delegate to the Consumer class. Merely conforming to it won't cut it. Declare it in -viewDidLoad by creating an instance of Consumer, possibly like this:
-(void)viewDidLoad {
Consumer *consumer = [[Consumer alloc]init];
[consumer setDelegate:self];
}
You also don't declare a property for your delegate object in Consumer, so it can never actually be accessed. Do this first:
#class Customer;
#protocol CustomerDelegate <NSObject>
-(void) didSelectCustomer:(Customer *) customer;
#end
#interface Customer : NSObject
{
}
#property (nonatomic,copy) NSString *name;
#property (nonatomic,copy) NSString *occupation;
#property (weak) id <CustomerDelegate> delegate; //use assign or __unsafe_unretained if targeting <5.0.
#end
You can check if your class conforms to your protocol like so:
if (![delegate conformsToProtocol:#protocol(CustomerDelegate)]) {
[NSException raise:#"Delegate Exception"
format:#"Parameter does not conform to CustomerDelegate protocol at line %d", (int)__LINE__];
}
the split view controller's last object.
this object is return a UI navigation controller.
you know, then you can do yourself.

Pass parameter when initializing table

Okay, I'm totally stumped here.
This works in CouponListViewController.m:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.couponList = [CouponDatabase database].couponList;
self.title = #"Coupon List";
}
And this works in CouponDetailViewController.m:
- (void)viewWillAppear:(BOOL)animated {
CouponDetails *details = [[CouponDatabase database] couponDetails:_uniqueId];
if (details != nil) {
[_merchantNameLabel setText:details.merchantName];
[_longDealLine1Label setText:details.longDealLine1];
//....blah...blah//
}
}
But when I change the CouponDatabase.h from this (which works with the above):
#class CouponDetails;
#interface CouponDatabase : NSObject {
sqlite3 *_database;
}
+ (CouponDatabase *)database;
- (NSArray *)couponList;
- (CouponDetails *)couponDetails:(int) uniqueId;
...to this (which works if I manually set the value of 'selectedCategory' inside the method):
#class CouponList;
#class CouponDetails;
#interface CouponDatabase : NSObject {
sqlite3 *_database;
}
+ (CouponDatabase *)database;
- (CouponList *)couponList:(int) selectedCategory;
- (CouponDetails *)couponDetails:(int) uniqueId;
and then change CouponListViewController.m to this:
1 - (void)viewWillAppear:(BOOL)animated {
2 [super viewWillAppear:animated];
3 self.couponList = [[CouponDatabase database] couponList:_selectedCategory];
4 self.title = #"Coupon List";
5 }
I get this error on line 3 above:
warning: incompatible Objective-C types 'struct CouponList *',
expected 'struct NSArray *' when passing argument 1 of 'setCouponList:'
from distinct Objective-C type
Question: What is the proper formatting of the 'self.couponlist' line so that I can pass an integer to the CouponDatabase for use in the couponList method?
EDIT: I'm aware that couponDetails is now a class instead of an array - I just don't know know how to format the line to initialize the table data.
I hope this makes sense - any help on this would be very greatly appreciated.
Thanks in advance!
Adding CouponListViewController.h:
#import <UIKit/UIKit.h>
#class CouponDetailsViewController;
#interface CouponListViewController : UITableViewController {
NSArray *_couponList;
CouponDetailsViewController *_couponDetails;
int _selectedCategory;
}
#property (nonatomic, retain) NSArray *couponList;
#property (nonatomic, retain) CouponDetailsViewController *couponDetails;
#property(nonatomic, assign) int selectedCategory;
#end
Try changing your CouponListViewController.h to this:
#import <UIKit/UIKit.h>
#class CouponDetailsViewController;
#interface CouponListViewController : UITableViewController {
CouponList *_couponList;
CouponDetailsViewController *_couponDetails;
int _selectedCategory;
}
#property (nonatomic, retain) CouponList *couponList;
#property (nonatomic, retain) CouponDetailsViewController *couponDetails;
#property(nonatomic, assign) int selectedCategory;
#end
Oops I put my response in my own original post and should have put it here:
Edit: Okay, I made changes to CouponListViewController.h as recommended by Robert, plus added #class CouponList; as follows:
#import <UIKit/UIKit.h>
#class CouponList;
#class CouponDetailsViewController;
#interface CouponListViewController : UITableViewController {
CouponList *_couponList;
CouponDetailsViewController *_couponDetails;
int _selectedCategory;
}
#property (nonatomic, retain) CouponList *couponList;
#property (nonatomic, retain) CouponDetailsViewController *couponDetails;
#property(nonatomic, assign) int selectedCategory;
#end
I'm still getting errors in CouponListViewController.m:
#import "CouponListViewController.h"
#import "CouponDatabase.h"
#import "CouponList.h"
#import "CouponDetailsViewController.h"
#implementation CouponListViewController
#synthesize couponList = _couponList;
#synthesize couponDetails = _couponDetails;
#synthesize selectedCategory = _selectedCategory;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.couponList = [CouponDatabase database].couponList; // <--- ERROR 1
self.title = #"Coupon List";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_couponList count]; // <--- WARNINGS 1 AND 2
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.couponDetails == nil) {
self.couponDetails = [[[CouponDetailsViewController alloc] initWithNibName:#"CouponDetailsViewController" bundle:nil] autorelease];
}
CouponList *info = [_couponList objectAtIndex:indexPath.row]; // <--- WARNING 3
NSLog(#"%#", info.uniqueId);
_couponDetails.uniqueId = info.uniqueId;
[self.navigationController pushViewController:_couponDetails animated:YES];
}
ERROR 1: request for member 'couponList' in something not a structure or union
WARNING 1: 'CouponList' may not respond to '-count'
WARNING 2: return makes integer from pointer without a cast
WARNING 3: 'CouponList' may not respond to '-objectAtIndex:'
In you original code for CouponDatabase, you are changing the definition:
- (NSArray *)couponList;
for this one:
- (CouponList *)couponList:(int) selectedCategory;
Nevertheless, you use that return value as datasource for a list view controller, so . Here you have a mismatch you should fix. How to fix it depends on the semantics of your application. What are your trying to do with - (CouponList *)couponList:(int) selectedCategory;? What does really return this selector? What is the interface CouponList? Possibly you should change the line:
self.couponList = [[CouponDatabase database] couponList:_selectedCategory];
so that it returns an NSArray build from a CouponList. But I am not sure of the semantics of your objects, so this might not be the case.

#protocol on IOS parsing data 2 way?

I have login Code like this :
#protocol LoginDelegate
-(void)DUsername:(NSString *) username DPassword:(NSString *) password;
#end
#interface loginipad : UIViewController {
id<LoginDelegate> delegate;
IBOutlet UITextField *edusername;
IBOutlet UITextField *edpassword;
}
and then i use this object on mainViewController like this :
#interface mainViewController : UIViewController<LoginDelegate> {
and call this methode on mainViewController
-(void)DUsername:(NSString *) username DPassword:(NSString *) password{
userlogin=[username retain];
passlogin=[password retain];
if (!scm.isRunning) {
[scm connectToHost:#"localhost" onPort:8080];
}
}
This method is success to parsing data from login modalview to mainViewController, but i want show progress of process or any message from mainViewController to login modal view when login button is klick (i try MBPrgoressHUD but no success due i use this login on modal view).
My Question how i can parsing data from mainViewController to This login modalview ?
Thanks,
for call the method :
loginipad *plogin = [[loginipad alloc] initWithNibName:#"loginipad" bundle:nil];
plogin.delegate = self;
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:plogin];
plogin.title=#"Login";
[self presentModalViewController:nc animated:YES];
[nc release];
nc = nil;
[plogin release];
plogin = nil;
answer completely edited
Your question leads to multiple solutions and strategies.
First: general posibilities to implement bidirectional data-transfer between two classes.
via multiple protocols: loose cupling but leads to import-loops which are annoying. I know ho to solve import loops for class-definitions (#class) but I dont know how to solve this for protocols
A.h:
#import "B.h"
#protocol ADelegate
-(void) adelegate:(NSString*)data;
#end
#interface A : NSObject<BDelegate>
{
id<ADelegate> delegate;
}
#end
B.h:
#import "A.h"
#protocol BDelegate
-(void) bdelegate:(NSString*)data;
#end
#interface B : NSObject<ADelegate>
{
id<BDelegate> delegate;
}
#end
via a single protocol: dense cupling :( but no import-loop (this is a working ugly style)
A.h:
//no import here needed
#protocol ADelegate
-(void) adelegate:(NSString*)data;
#end
#interface A : NSObject<BDelegate>
{
id<ADelegate> delegate;
}
#end
B.h:
#import "A.h"
#interface B : NSObject<ADelegate>
{
A* delegate;
}
#end
via pipe/stream: bidirectional data-transfer should by done using a pipe (unbuffered) or stream (buffered)
here I show you a small and simple delegate-pipe but there also exists a NSPipe/NSStream
DelegatePipe.h
#protocol DelegatePipeDelegate
- dataArrived:(NSString*)data;
#end
#interface DelegatePipe : NSObject {
NSMutableArray *delegates;
}
-(void)open:(id<DelegatePipeDelegate>)d;
-(void)close:(id<DelegatePipeDelegate>)d;
-(void)send:(NSString*)data;
#end
DelegatePipe.m
#implementation DelegatePipe
-(id)init
{
if(self = [super init])
{
delegates = [NSMutableArray array];
}
return self;
}
-(void) dealloc
{
[delegates release];
delegates = nil;
}
-(void) open:(id <DelegatePipeDelegate>)d
{
#synchronized(self)
{
if([delegates containsObject:d])
return;
//if([delegates count]>=2) //Pipe contains originally only 2 delegates. but a broadcaster is also nice ;)
// return;
[delegates addObject:d];
}
}
-(void) close:(id <DelegatePipeDelegate>)d
{
#synchronized(self)
{
[delegates removeObject:d];
}
}
-(void) send:(NSString *)data
{
#synchronized(self)
{
for(id<DelegatePipeDelegate> d in delegates)
[d dataArrived:data];
}
}
#end
Second: KVO
KVO is often used in a ModelViewController (MVC) Pattern. eg: visualize data in a view. The network-connection-state in your case is part of data and your loginipad is a view (and a controller)
Authentificator.h
typedef enum eAuthState
{
NOT_CONNECTED = 0,
LOGIN_FAILED,
CONNECING,
CONNECTED
} AuthState;
#interface Authentificator : NSObject {
AuthState state;
}
#property (nonatomic, assign) AuthState state;
#end
Authentificator.m
...
-(void) doAuthWithUsername:(NSString*)name password:(NSString*)pw
{
self.state = CONNECING;
//do network-stuff
}
//delegate from network. here NSURLConnection
-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
//parse network-answer
BOOL success = YES;
if(success)
self.state = CONNECTED;
else
self.state = LOGIN_FAILED;
}
loginipad.h
#interface loginipad : UIViewController
{
Authentificator *auth;
}
#property (nonatomic, retain) Authentificator *auth;
#end
loginipad.m
#implementation loginipad
#synthesize auth;
//override setter for more comfortable use (add/removeObserver)
-(void) setAuth:(Authentificator *)a
{
#synchronized(auth)
{
[auth removeObserver:self forKeyPath:#"state"];
[auth release];
auth = a;
[auth retain];
[auth addObserver:self forKeyPath:#"state" options:0 context:nil];
}
}
-(IBAction) buttonClicked:(id)aSender
{
self.auth = [Authentificator sharedAuthentificator];
}
-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if(![object isKindOfClass:Authentificator.class])
return;
AuthState state = ((Authentificator*)object).state;
NSLog(#"curState: %i",state);
//do sth with state
}
- (void)dealloc {
self.auth = nil;
[super dealloc];
}

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.