user2967559
user2967559

Reputation: 97

Hide value inside viewcontroller on UIWebView from another viewcontroller

I have viewcontroller A, B, C. viewcontroller A having UIWebView for displaying HTML pages and it highlight string values on UIWebView. For requirement, When i coming from viewcontroller B, viewcontroller A need to display the highlighting text on UIWebView. When i coming from viewcontroller C, viewcontroller A no need to display the highlighting text. But when i coming from viewcontroller B & C, the viewcontroller A displaying the highlight text. How to hide the highlight text on UIWebView while coming from viewcontroller C. Is it possible? How to know i'm coming from viewcontroller B or C? If it's find then maybe have a solution

viewcontroller A :

 - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.

      wbCont = [[UIWebView alloc] initWithFrame:CGRectMake(0, 45, 320, 568)];


      [wbCont loadHTMLString:webString baseURL:nil];

        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        // getting an NSString
        NSString  *savedValue = [prefs stringForKey:@"got"];


       if (self.displayHighlightedText) {


                if(savedValue){


[self highlightAllOccurencesOfString1:savedValue];

}


}

[self.view addSubview:wbcont];

viewcontroller B :

viewcontroller B *det = [[viewcontroller B alloc] init];

    NSString *decodeString = [self htmlEntityDecode:content];


    [content release];

    det.webString = decodeString;

    [self.navigationController pushViewController:det animated:YES];

viewcontroller C :

viewcontroller c *det = [[viewcontroller C alloc] init];

    NSString *decodeString = [self htmlEntityDecode:content];


    [content release];

    det.webString = decodeString;

    [self.navigationController pushViewController:det animated:NO];

Upvotes: 0

Views: 139

Answers (3)

iSmita
iSmita

Reputation: 1292

Just pass BOOL flags from one viewController to next viewController.It will solve your problem.

In AViewController while switching from AViewController to BViewController pass BOOL flag like this -

BViewController *aViewController = [[BViewController alloc]init];
bViewController.passFlag = TRUE;
[self.navigationController bViewController animated:YES];

But declare this in BViewController.h -

@property(nonatomic)BOOL passFlag;

and in BViewController.m -

@synthesize passFlag;

Now, in BViewController check -

if(passFlag){
  // highlight text
}else{
  // remove highlighting text
}

In this way you can pass BOOL flags from BViewController to CViewController.

Hope this will helps you.Thank you.

Upvotes: 1

kyurkchyan
kyurkchyan

Reputation: 2428

One possible solution can be using NSNotificationCenter. Idea is posting notifications when you travel from view controller C or B to A with the class name of source view controller encapsulated in it, so the receiver of the notification (in this case it will we view controller A) can read the class and determine what to do when the notification is received. Here is some code to do the job

    //In your B or C view controller exactly before you travel from them to A.
    //If you use UINavigationController and your root controller is A, then you'll have something similar to this
    //In case you are in view controller "B"
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ViewControllerChanged" object:@"B"];
    [self.navigationController popToRootViewControllerAnimated:YES];

//In your A view controller you'll have something similar to this
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewControllerChanged:) name:@"ViewControllerChanged" object:nil];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super viewWillDisappear:animated];
}

-(void)viewControllerChanged:(NSNotification *) notification
{
    NSString * controllerName = notification.object;
    if([controllerName isEqualToString:@"B"])
    {
        //Put code to show the highlighted text here
    }
    else if([controllerName isEqualToString:@"C"])
    {
        //Put code to hide the highlighted text here
    }
}

Upvotes: 0

giorashc
giorashc

Reputation: 13713

You can define a BOOL property for view controller A which tells it if to display highlighted text or not. Add the following to your view controller A interface .h file:

@property (nonatomic) BOOL displayHighlightedText;

When you push/modal your A view controller set the property to the required value (i.e. from view controller C set to false and from B set to true)

Use this property in your viewDidLoad method of view controller A :

if (self.displayHighlightedText) {
   [self highlightAllOccurencesOfString1:savedValue];
}

In your edited code for viewcontroller C add before pushing det view controller:

det.displayHighlightedText = NO;

In your edited code for viewcontroller B add before pushing det view controller:

det.displayHighlightedText = YES;

Upvotes: 0

Related Questions