How can I declare an object globally and then use it in a method - global

I declared a method UISearchBar *searrchBar Globally and also synthesized it.
I've created a table view in which under cell.textLabel.text I would like to use this searrchBar, how can I do it
Thanks

int abc=0;
void method(){
abc++;//do what ever you want
}

Related

Add a property to all view controllers in an iPhone app

View controllers in my iPhone app either extend from UIViewController or UITableViewController. I need to add a property to all of them so that I can pass user information between controllers. The solution I am aware of is to add a BaseViewController and a BaseTableViewController, add the property to both of them, then make all controllers to inherit from them instead.
Repeating the property in BaseViewController and BaseTableViewController doesn't seem to be the most elegant solution to me. Is there any better ones?
Thanks!
As extensions will not work in your case, you could write a category on UIViewController and use objc_getAssociatedObject to store the data you want and retrieve it with objc_getAssociatedObject. But you should use this option with care.
static NSString *key = #"example";
- (void)setExampleProperty:(id)value {
objc_setAssociatedObject(self, (__bridge void *)key, value, OBJC_ASSOCIATION_RETAIN);
}
- (id)exampleProperty {
return objc_getAssociatedObject(self, (__bridge void *)key);
}
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html

Share a bool variable / NSNUmber between two view controllers

I have two view controllers and I want to share a bool variable between them.
So I create a bool variable with a #propery (nonatomic, assign) on both sides and on the one side I wrote
newVC.myBool1 = self.myBool2;
On the other view controller I can read the value of the passed bool variable, but I need to change it at the second view controller so I can read the value at the first view controller.
So I know, this is not possible, because `bool* it is a primitive type.
So I used NSNumber, but this also does not work. On the first view controller I set on viewDidLoad
self.myBool1 = [NSNumber numberWithBool:NO];
On the second view controller:
self.myBool2 = [NSNumber numberWithBool:YES];
But on the first view controller the value is 0 - NO... So it seems that creating the new NSNumber is not shared to the first view controller.
What can I do to solve this problem?
Regards Tim
You have lots of choices, but which you should use depends on whether both viewControllers need notification of when the value changes.
If you don't need notification, the easiest choice is to use a global BOOL variable, although purists will scoff at the suggestion. But really it's two lines of code and you're done. Another option would be to store the value in NSUserDefaults.
If you need change notification in each viewController, perhaps the cleanest design is to write a "set" method in one viewController that sets the value in both itself and the other viewController. Something like:
-(void) setMyBool:(BOOL)newValue
{
myBool = newValue;
otherViewController.myBool = newValue;
}
If you want to change the value from either viewController, it gets a little trickier because you have to have each viewController keep a reference to the other and make sure not to recurse when setting the value. Something like:
-(void) setMyBool:(BOOL)newValue
{
if ( self.busyFlag == YES )
return;
self.busyFlag = YES;
myBool = newValue;
otherViewController.myBool = newValue;
self.busyFlag = NO;
}
Yet another option would be to use NSNotifications to change the value and have each viewController class listen for the change notification. And TheEye's suggestion of writing a wrapper class and keeping a reference to an instance of that class in both viewControllers would work too.
If you don't need change notifications, though, I would just create a global BOOL variable and get on with the rest of the application because it's so easy, reliable and hard to mess up.
An NSNumber object is immutable, so you can't use it like that. If you write [NSNumber initWithxxx], in fact you create a new object.
If you want to share a number or boolean between several classes, you should create your own wrapper class with setters and getters for the bool value (or subclass NSNumber). This class you can share between classes.

Cant access an int value

i declared an int value as my imageIndexForSend through the following code in myView class.
int imageIndexForSend;
#property int imageIndexForSend;
#synthesize imageIndexForSend;
after on a button click i am displaying a popover which is PopOver calss.
there is table view with multiple indexes in popover class.when i click on any row in PopOver class table it set myView class imageIndexForSend as
In PopOver
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
myView *obj = [[myView alloc] init];
[obj getImageForSend:indexPath.row];
[staticSceneController release];
}
in myViewClass
-(void)getImageForSend:(int)index{
imageIndexForSend = index;
}
then i am return to myViewClass after dismissing popover (popOver class) and doing some actions in myViewClass.
then i am clicking a send button.but the integer value imageIndexForSend is zero.cant get the old value which i set from PopUp.
can any one tell me a way to get the old imageIndexForSend value.may i know what mistake i done.
First, you name a method with get to set the value, it's bad.
Second, you use a property and synthesize it, so you don't need to rewrite the set method unless you need to have a custom set method.
And finally you create a new view on each selection of tableview cell !
1) Remove your getImageForSend: method, you don't need that with property
2) Instead using : [obj getImageForSend:indexPath.row];, use : obj.imageIndexForSend = indexPath.row;
3) Don't create a new view on each selection, assign the value on the existing view.
A better way to transmit data from your popover to your view (controller ?) is to have a delegate property in your popover class and set it with your view object, create a delegate protocol with a method that is called when a cell is selected in popover with an int argument (the index) then make your view class adopts the protocol and do a obj.imageIndexForSend = argument; in your protocol method.
It seems you're allocating a myView instance and assigning that to a local variable (obj), but then you don't keep a pointer to that new instance anywhere.
From what I understand, you already have an existing instance of myView, so what you need to do is to set the variable on that instance, and not create a new one every time.
Each instance have their own set of variables, so changing it in a new instance won't affect any other instances.
You are instantiating a new MyView whenever the user taps on any row of your UITableView. You should try to access the original MyView instead (or whatever object shall retain that setup value).
Within your popover, you should find a way to access the instance that holds the actual index-value. How exactly that is achieved depends a lot on your implementation.
In other words, do not instantiate something within an object that has a shorter lifetime than the object that will access that very instance.
If you're trying to access the index of the selected row in the UITableView you can just use the following:
int index = [myTableView indexPathForSelectedRow].row;

Objective-C: How Can I Access String Variable As a Global?

I am new to iPhone development. I want to access a string variable in all the class methods, and I want to access that string globally. How can I do this?
Please help me out.
Leaving aside the issue of global variables and if they are good coding practice...
Create your string outside of any Objective-C class in a .m file in your project:
NSString *myGlobalString = #"foo";
Then put the declaration in a header file that is included by every other file that wants to access your string:
extern NSString *myGlobalString;
OK, well I can't leave it entirely aside. Have you considered putting your "global" string somewhere else, perhaps inside your application delegate as a (possibly read-only) property?
The preferred methods for creating a global variable are:
Create a singleton class that stores
the variables as an attributes.
Create a class that has class methods that return the variables.
Put the class interface in the
universal header so all classes in
the project inherit it.
The big advantage of method (2) is that it is encapsulated and portable. Need to use classes that use the global in another project? Just move the class with the variables along with them.
You can achieve that by implementing getter and setters in the delegate class.
In delegate .h file
Include UIApplication delegate
#interface DevAppDelegate : NSObject <UIApplicationDelegate>
NSString * currentTitle;
- (void) setCurrentTitle:(NSString *) currentTitle;
- (NSString *) getCurrentTitle;
In Delegate implementation class .m
-(void) setCurrentLink:(NSString *) storydata{
currentLink = storydata;
}
-(NSString *) getCurrentLink{
if ( currentLink == nil ) {
currentLink = #"Display StoryLink";
}
return currentLink;
}
So the variable you to assess is set in the currentlink string by setters method and class where you want the string ,just use the getter method.
All the best
I posted an article about my methodology for doing this:
http://www.pushplay.net/2011/02/quick-tip-global-defines/
This is something I primarily use for notification keys. Creating a globals.h file and adding it to the (your_project_name)_Prefix.pch file will ensure it is accessible globally...

Why does UITextView use string rather than mutable string for its text property?

Any idea from a code design standpoint why does the internal implementation of UITextView uses an NSString but not an NSMutableString when its content is meant to change often?
From a general coding point of view:
When setting a property the property setter method is called. That way the control is able to notice when the property is changed, so that it can redraw the control with the new content.
If the property is a mutable object, you can change its contents and the control will not get any notification that this has happened, so it doesn't know that the control needs to be redrawn.
It's a general pattern in Cocoa to pass around immutable objects instead of allowing outside classes access private mutable variables. You'll see the same thing with collections classes like NSArray and NSDictionary.
Of course, there's no reason you can't change what it points to! Because the member is just a pointer, you can replace the string with an NSMutableString yourself if you want.
This might be a more efficient approach if you want to append a lot of text to the view.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[myTextView setText:[[NSMutableString alloc] init]];
}
return self;
}
Just be sure to still call setText to because as #Guffa explained in his answer, otherwise the view won't know to redraw itself.
- (void)appendText:(NSString*)text
{
NSMutableString *dispText = (NSMutableString*)[myTextView text];
[dispText appendString:text];
[myTextView setText:dispText]; // notify myTextView of text change!
}