0

I have a blue line in my app that displays an artificial horizon. I want to be able to display and remove it at different parts of the code (.m). Can I declare it (lineViewHorizon) universally.

At present I can make it appear/disappear only within the same section/method of code.

I presume this can be done?

UIView *lineViewHorizon = [[UIView alloc] initWithFrame:CGRectMake(0, pageTopMargin+inthorizon, self.view.bounds.size.width, 2)];
lineViewHorizon.backgroundColor = [UIColor blueColor];
[self.view addSubview:lineViewHorizon];
[lineViewHorizon removeFromSuperview];

1 Answer 1

1

I want to be able to display and remove it at different parts of the code

In order to remove a view from another view, you'll need a pointer to the view that you want to remove. You can get that either by finding the view in the view hierarchy, perhaps using -viewWithTag:, or you can keep a pointer to the view in an instance variable (or property). Either way, the key thing is that you need a pointer to the view so that you can send it a -removeFromSuperview message.

Sign up to request clarification or add additional context in comments.

2 Comments

I was trying the second approach but am grappling with the syntax. I have put a -(void) Property in my .h but not sure how to declare the pointer in the .m.
Can I declare this as UIView *lineViewHorizon; after the @implementation? That works but is it good practice?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.