iMubarak
iMubarak

Reputation: 462

How to set NSString before calling a method objective c

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.

Upvotes: 0

Views: 220

Answers (1)

Chris Trahey
Chris Trahey

Reputation: 18290

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:

  1. Create a @protocol ColorPickerDelegate with one method: -(void) userChoseColor:(UIColor *) color
  2. Add a @property (weak) id<ColorPickerDelegate> delegate to your color picker view controller
  3. make your VC here implement that protocol
  4. Inside the delegate method, set your local property
  5. Implement a custom setter for the local propert, and update the background color whenever the color changes.

Upvotes: 1

Related Questions