Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design pattern for UI controls' state management in iOS

Similar to this question, but I am looking for a generic solution or design pattern or framework.

Q. How to add state management into all UI controls in my iOS app automatically without the need to rewrite the existing controls' class?

Example:

e.g. When I click on a UIButton, it will create a new UIWebView showing Google home page. That is easy, but problem arise when user sometimes.. click the button just too fast, so two webview will be displayed.

To solve this question, I would need to make a singleton class which contain the webview, and have a state variable isOpended and if it is true, reuse the existing webview instead of creating a new one.

But the problem is: If I want this behavior in other controls also, then I would need to create many many singleton classes..I am just thinking if there is better way to handle this without the new to re-invent the wheel.

Thanks.

like image 431
Ryan Avatar asked Jun 03 '12 17:06

Ryan


People also ask

What are the design patterns used in iOS?

Most common Cocoa design patterns: Creational: Singleton. Structural: Decorator, Adapter & Facade. Behavioral: Observer and Memento.

Which design pattern is best for iOS development?

MVC Design Pattern The Model View Controller or MVC is considered to be a crucial design pattern in iOS app development. Using this pattern means that the application contains data model, presentation, and control info. This pattern divides each object.

What is state management in iOS?

State management is the root of most headaches for native mobile development - as well as single page app web development. Mobile applications are almost always stateful. On top of keeping track of the current state in business flows, you need to make sure you handle app lifecycle transitions (iOS, Android).

How many design patterns are there in Swift?

Here we are going to discuss the five creational design patterns and how we can use them in swift.


2 Answers

I think you're solving the wrong problem here. Why don't you disable the button until the UIWebView is done processing. That way the user cannot click it twice.

- (IBAction)showMapHomepage:(UIButton*)sender
{
    sender.enabled = NO;
    [self taskThatTakesALongTimeWithCompletion:^{
        sender.enabled = YES;
        // Finish processing
    }];
}
like image 99
Jeffery Thomas Avatar answered Oct 28 '22 04:10

Jeffery Thomas


You are misinterpreting the best way to go about solving your problem. First of all, you should never find yourself in a situation where you are creating many many singletons. Singletons are a necessary evil, but you should not overuse nor abuse them. Here is a good post about singletons in objective-c.

There are numerous ways you could go about preventing a second UIWebView from being displayed when the user clicks your button.

As someone else stated, one solution would be to disable the button so that the user cannot "double-click" it. You do this using:

button.enabled = NO;

You could also hide your button using:

button.hidden = YES;

Or, in the header of the class that contains your UIButton, you could create a boolean that will handle the logic of whether or not the button has been pressed;

// declare this in your header
BOOL buttonPressed;

// this is the IBAction that your button hooks up to
- (IBAction)createWebViewButtonPressed:(id)sender {
    if(!buttonPressed) {
        buttonPressed = YES;
        // insert code here to create your UIWebView
    }
}

Again, there are numerous ways to accomplish what you are trying to do. You just have to determine which method is the best for you.

like image 21
Michael Frederick Avatar answered Oct 28 '22 04:10

Michael Frederick