How to set NSString before calling a method objective c - iphone

It might be a simple question yet I could not figure out what I am missing.
In ViewControl.h I declared UIColor
#property (nonatomic, strong) UIColor * myColor;
In ViewControl.m I have a method that do something and return new UIColor
#synthesize myColor = _myColor;
In ViewDidLoad Method
- (void)viewDidLoad
{
myColor = [UIColor RedColor];
}
-(void) ShowColorPopUpView
{
if (!self.wePopoverController)
{
ColorViewController *contentViewController = [[ColorViewController alloc] init];
contentViewController.delegate = self;
self.wePopoverController = [[WEPopoverController alloc] initWithContentViewController:contentViewController];
self.wePopoverController.delegate = self;
self.wePopoverController.passthroughViews = [NSArray arrayWithObject:self.navigationController.navigationBar];
[self.wePopoverController presentPopoverFromRect:self.tvTweetDetails.frame
inView:self.view
permittedArrowDirections:(UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown)
animated:YES];
} else
{
[self.wePopoverController dismissPopoverAnimated:YES];
self.wePopoverController = nil;
}
}
-(void) colorPopoverControllerDidSelectColor:(NSString *)hexColor
{
_myColor = [GzColors colorFromHex:hexColor];
[self.view setNeedsDisplay];
[self.wePopoverController dismissPopoverAnimated:YES];
self.wePopoverController = nil;
}
- (UIColor *) returnColor
{
return _myColor;
}
My Question starts here: I have two methods to change a textview font and background color
- (IBAction)btnFontColorPopUpMenu:(id)sender
{
[self ShowColorPopUpView];
tvTweetDetails.textColor = [self returnColor];
}
- (IBAction)btnTextViewBackGroundColor:(id)sender
{
[self ShowColorPopUpView];
tvTweetDetails.backgroundColor = [self returnColor];
}
The issue now is when I call the method it return it returns RED and if I call it again it returns the the BlackColor.
How Can I call the method and change the Color to the new one and then return it. I want to get the Black color directly.
I want to execute the method first then return the color but what happens is assign the color before execute the method.
I hope I made it the Question Clear.

Okay, I'll take a whack at this.
I suspect you are doing some kind of presentViewController:... method in your color changer method. That's great, but it has implications. The method you call that in continues to execute during that presentation. That means it may return, etc.
This is where the concept of delegates comes in. You may benefit from restructuring the data flows here a bit.
What I suggest (if I am correct about the presentation of a color picker UI) is that you make the following changes:
Create a #protocol ColorPickerDelegate with one method: -(void) userChoseColor:(UIColor *) color
Add a #property (weak) id<ColorPickerDelegate> delegate to your color picker view controller
make your VC here implement that protocol
Inside the delegate method, set your local property
Implement a custom setter for the local propert, and update the background color whenever the color changes.

Related

Call method only once every app launch

Hey guys at Stackoverflow!
I need your help. I am looking for a way to call my method only once after the application has launched and "save" the state of the color of the UIView. At first I will show you my code, that I can explain it in a better way:
-(void)viewWillAppear:(BOOL)animated {
NSArray *colors = [NSArray arrayWithObjects:[UIColor myWhiteColor],[UIColor myBlueColor],[UIColor myCyanColor],[UIColor myGreenColor],[UIColor myOrangeColor],[UIColor myPurpleColor],[UIColor myRedColor],[UIColor myViolettColor],[UIColor myYellowColor], nil];
NSInteger randomIndex = random() % [colors count];
colorTransparentView.backgroundColor = [colors objectAtIndex:randomIndex];
colorTransparentView.opaque = NO;
colorTransparentView.alpha = 1.0;
}
Now I explain you my issue.
As you can see, the code above changes the color of the UIView, every time the "viewWillAppear" method was called. The code randomly changes the color of the UIView(in the .xib) which is linked with an IBOulet to the header file. The problem is that every time I get back to the view I will get a different color.
However, I want to set the random color of the UIView only once the application started. This color should stay until the application was closed from the Multitasking. I can't see any way to solve this. I tried to call the code in the applicationDidFinishedLaunchingWithOptions method, but I wasn't succsessful.
Also I tried the dispatch_once method to call it only once, but as you may think of the color was never called again, so the view got no color the second time loaded.
I really would appreceate if you could help me with this.
Thanks in Advance,
Noah
EDIT:
My header:
#interface ViewController : UIViewController {
IBOutlet UIView *colorTransparentView;
}
#end
What about using a static variable? initialise it with 0 and after changing color in your view will appear. set it to 1 and keep checking it.
int static colortaken = 0;
int static colorindex;
- (void)viewWillAppear:(BOOL)animated
{
NSArray *colors = [NSArray arrayWithObjects:[UIColor myWhiteColor],[UIColor myBlueColor],[UIColor myCyanColor],[UIColor myGreenColor],[UIColor myOrangeColor],[UIColor myPurpleColor],[UIColor myRedColor],[UIColor myViolettColor],[UIColor myYellowColor], nil];
if (colortaken == 0)
{
NSInteger randomIndex = random() % [colors count];
colorindex = randomIndex;
colorTransparentView.backgroundColor = [colors objectAtIndex:randomIndex];
colorTransparentView.opaque = NO;
colorTransparentView.alpha = 1.0;
}
else
{
// do nothin
colorTransparentView.backgroundColor = [colors objectAtIndex:colorindex];
colorTransparentView.opaque = NO;
colorTransparentView.alpha = 1.0;
}
// at end
colortaken = 1;
}
Use dispatch_once. You can read up on singleton approaches in general, but this is the recommended approach.

in -init method won't change subviews

I have a object derived from UIView, it is AIItem, this item have UIImageView *status_view, now I need another object AIAnotherItem derived from AIItem, problem is in status_view.
For Example :
AIItem init method
-(id)initWithName:(NSString *)name {
self = [super init];
if (self) {
status_view = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,50,50)];
status_view.image = [UIImage imageNamed:#"item_image.png"];
[self addSubview:status_view];
}
}
AIAnotherItem init method
-(id)initWithName:(NSString *)name {
self = [super initWithName:name];
if (self) {
status_view.image = [UIImage imageNamed:#"another_item_image.png"];
}
return self;
}
in AIAnotherItem I set another image to status_view but it won't changed.
Question is why ? and how do this ?
Regardless what the mechanics are of this not working (I am sure you will figure it out), I believe that you are perhaps not going about this the right way.
Would it not be more logical to have class AIItem.h that has an empty property statusView? And then two derived classes (or instances of the same subclass) that inherit the same statusView but fill it with different images?
I think this would correspond more closely to the philosophy behind inheritance.

iOS - Can not set Button 'Disable', without removing the content of it

Sorry for what should be a simple solution.
I am trying to 'Disable' a button in code using the following Code:
in the first Class which Called 'CheckBox':
checkBox.h :
#interface CheckBox : UIViewController
{
BOOL checkboxSelected;
UIButton *checkBoxButton;
}
#property (nonatomic, retain) IBOutlet UIButton *checkBoxButton;
- (IBAction)checkBoxButton:(id)sender;
-(void) setCheckBoxSelected:(BOOL)checkingStatus;
-(void) setCheckBoxEnabled:(BOOL)enablingStatus;
in checkBox.m :
- (IBAction)checkBoxButton:(id)sender {
if (checkboxSelected == 0){
[checkBoxButton setSelected:YES];
checkboxSelected = 1;
} else {
[checkBoxButton setSelected:NO];
checkboxSelected = 0;
}
}
-(void) setCheckBoxSelected:(BOOL)checkingStatus {
checkBoxButton.selected = checkingStatus;
}
-(void) setCheckBoxEnabled:(BOOL)enablingStatus {
[checkBoxButton setEnabled:enablingStatus];
}
and in the implementation of another class which called 'MainViewController.m' :
- (void)viewDidLoad{
allTransactionCheckBox = [[CheckBox alloc] init];
[self.viewWithdraw addSubview:withdrawCheckBox.view ];
withdrawCheckBox.labelCheckBox.textColor = [UIColor blackColor];
withdrawCheckBox.labelCheckBox.font = [UIFont systemFontOfSize:14];
withdrawCheckBox.labelCheckBox.text = #"Withdraw";
[withdrawCheckBox setCheckBoxSelected:YES];
}
The above code is 'Disable' the button, but it (Remove/ Hide) the 'check mark Picture' inside the button.Why?
All links ( outlet + Actions ) are connected .
What obvious thing am I missing? Thanks.
What exactly are you trying to do? Maybe setting userInteractionEnabled is what you want?
Cheers
You have defined the button in interface builder. It looks like you havent set the image for all control states - select the button and look at the attributes inspector - there is a drop down for normal, highlighted, etc. is your check mark image defined for all those states?
This looks way too complex for what you want to do. You have a reference to your button -- checkBoxButton. Just use checkBoxButton.enabled = NO;

Can I reuse colors in Interface Builder?

I've got several yellow buttons created using Inteface Builder. All have the same color. Currently I declare color in each xib. Can I declared it globally and reuse across all xibs?
Not possible in Interface Builder. Do it in code, for example by creating special subclass of the button.
You could use system Color Palette to save the color, but you still need to apply it to all buttons every time you decide to change it. Or you can just use Recently Used Colors in the color chooser, but neither way is enough dynamic.
Yes, you can do this.
At the bottom of the color picker popup in Interface Builder, there's a row of squares you can use to store colors for later use. Drag a color into it from the rectangle where the current color is shown at the top of the color picker to store it, and then just click a stored color later to use it.
I don't believe there is a way to do this entirely in interface builder, unfortunately. However, you can come pretty close with a little bit of code. The best way I've found to be able to change colors throughout the app in one go is to subclass the item that you want to color (UILabel, for instance) to set the color upon initialization:
#interface HuedUILabel : UILabel
#end
#implementation HuedUILabel
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
// Initialization code
self.textColor = [AppConfig primaryColor];
}
return self;
}
#end
Then, set the label to have a custom class in IB:
Now, when you want to change the color of all your UILabels, you can do it by changing your one color def AND you don't have to clutter your code with a bunch of appearance assignments.
Most definitely!
Create a singleton object (you can call it OksanaColor, to be cool)...
... or, if you're really lazy, a read-only UIColor property that you can access from your app delegate.
You can also add a category on UIColor, so you can use it same as when you use UIColor. For example in my App I add a new file called ApplicationColors which contains all my app colors.
#interface UIColor (ApplicationColours)
+(UIColor *)savaColor;
#end
Implementation:
#implementation UIColor (ApplicationColours)
+(UIColor *)savaColor
{
return [UIColor colorWithRed:228.0f/255.0f green:86.0f/255.0f blue:86.0f/255.0f alpha:1.0f];
}
#end
Then to use it in my app, I import the ApplicationColours.h and use the same as any other UIColor. i.e:
label.textColor = [UIColor savaColor];
Here's a very simple implementation of a named colors category for UIColor. With this code in your project, UIColor will remember any colors you want to save, and will let you access your own colors or system colors using +colorWithName:
#interface UIColor (namedColors)
+ (UIColor *) colorWithName:(NSString *) name;
+ (void) setColor:(UIColor *) color forName:(NSString *) name;
#end
static NSMutableDictionary *colorStorage;
#implementation UIColor (namedColors)
+ (NSMutableDictionary *) colorStorage
{
if (!colorStorage)
colorStorage = [[NSMutableDictionary alloc] initWithCapacity:10];
return colorStorage;
}
+ (UIColor *) colorWithName:(NSString *) name
{
UIColor *result =[[self colorStorage] valueForKey:name]; // See if we have a color with this name in the colorStorage.
if (result) return result;
SEL selector = NSSelectorFromString(name); // look for a class method whose selector matches the given name, such as "blueColor" or "clearColor".
if ([self respondsToSelector:selector] && (result = [self performSelector:selector]))
if ([result isKindOfClass:[self class]])
return result;
return nil;
}
+ (void) setColor:(UIColor *) color forName:(NSString *) name
{
[[self colorStorage] setValue:color forKey:name];
}
#end

How to share constants between Interface Builder and the code?

I wonder if there is a way to use constants in Interface Builder, in order to avoid manually setting the same color at different places for example (it could be a very tedious job sometimes...)
Currently I set the color in the code and use #define to setup the color, but obviously IB can't use #define...
I have worked around this issue by subclassing the various controls to ensure the same style throughout the app. The drawback is that you can not see the style in interface builder only a wireframe.
For example I have a
#interface MyButton : UIButton
#end
#implementation MyButton
-(void) initialize{
self.backgroundColor = [UIColor MyButonColor]; // Using a category on UIColor
}
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self initialize];
}
return self;
}
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder:decoder]) {
[self initialize];
}
return self;
}
I think the easiest way to do this would be to create a category on the UIColor class and create a class method on it. For example:
Place this in a header file (e.g. UIColor+CustomColors.h):
#interface UIColor ( CustomColors )
+ (UIColor *)myCustomColor;
#end
Place this in an implementation file (e.g. UIColor+CustomColors.m)
#implementation UIColor ( CustomColors )
+ (UIColor *)myCustomColor
{
return [UIColor colorWithRed:0.2 green:0.5 blue:0.2 alpha:1.0];
}
#end
Then you have access to the class method anywhere in your code like so:
...
self.view.backgroundColor = [UIColor myCustomColor];
...
See Apple's documentation on Categories for more info.
Alternatively, you can save swatches of color through the system color palette. To do this you simply call up the system color palette, select a color and drag it into the grid of colors.
These colors are now available in not only every Interface Builder document you create, but any application that makes use of the system color palette.
color palette http://img.skitch.com/20091030-dhh3tnfw5d8hkynyr7e5q3amwg.png