iOS Development--no net-occupied bitmap implementation

Source: Internet
Author: User

https://www.jianshu.com/p/d537393fe247

Https://github.com/wyzxc/CQPlaceholderView
Https://github.com/wyzxc/TableViewNoContentView

Let's take a look at the actual effect: effect. gif Why do I use a bitmap without a net?

For a better user experience. Do you show a blank template when you don't have a net? Of course, no net-occupied bitmap is also one of the most basic functions of the app only one.

Principle Introduction:

At the viewWillAppear time to determine whether there is a net, if not the network, the display of the bitmap. Click the re-view button on the bitmap to determine if there is a net, if there is no net, toast prompt. If there is a net, remove the no-net-occupied bitmap and reload the data.

Detailed Explanation:
    • First, the designer's UI diagram encapsulates a non-net-occupied bitmap view: CQNoNetworkView , in the construction method to complete
- (instancetype)initWithFrame:(CGRect)frame{    if (self = [super initWithFrame:frame]) { // UI搭建 [self setUpUI]; } return self;}
   /** UI Build */-(void) setupui{Self.backgroundcolor = [Uicolor Whitecolor];404 picture put in the middleUiimageview *nonetworkimageview = [[Uiimageview Alloc]initwithframe:CGRectMake (0,0,125,125)]; Nonetworkimageview.center =Cgpointmake (Self.width/2,Self.height/2); [Self Addsubview:nonetworkimageview]; Nonetworkimageview.image = [UIImage imagenamed:@ "404notfound"];Re-view buttonUIButton *checkbutton = [[UIButton Alloc]initwithframe:CGRectMake (0, Nonetworkimageview.maxy +24,115,30)]; Checkbutton.centerx =Self.width/2; [Self Addsubview:checkbutton]; [Checkbutton.titlelabel setfont:[Uifont systemfontofsize:15]]; Checkbutton.backgroundcolor = [Uicolor colorwithred:0.00 Green:0.76 Blue:0.66 Alpha:1.00]; [Checkbutton Settitle:@ "re-view" forstate:UIControlStateNormal]; [Checkbutton settitlecolor:[Uicolor Whitecolor] Forstate:UIControlStateNormal]; [Checkbutton AddTarget:Self action:@selector (checknetworkbuttonclicked) forControlEvents:UIControlEventTouchUpInside];Two labels above the pictureUILabel *label1 = [[UILabel Alloc]initwithframe:CGRectMake (0,0,100,20)]; Label1.Text =@ "Refresh back shop!"; Label1.font = [Uifont systemfontofsize:14]; Label1.textcolor = [Uicolor colorwithred:0.00 Green:0.77 Blue:0.68 Alpha:1.00]; Label1.backgroundcolor = [Uicolor Clearcolor]; Label1.textalignment =Nstextalignmentcenter; [Label1 SizeToFit]; Label1.centerx =Self.width/2; Label1.maxy = Nonetworkimageview.y-21; [Self Addsubview:label1];UILabel *label2 = [[UILabel Alloc]initwithframe:CGRectMake (0, 0, 100, 15)]; Label2.Text = @ "you seem lost"; label2.font = [uifont Systemfontofsize:19]; Label2.textcolor = [uicolor colorwithred:0.00 Green:0.77 blue:0.67 alpha:1.00]; Label2.backgroundcolor = [uicolor clearcolor]; label2.textalignment = nstextalignmentcenter; [Label2 SizeToFit]; Label2.centerx = self.width/2; label2.maxy = label1.y-12; [self Addsubview:label2];}          
    • To handle the logic of the "Re-view" button, the basic idea is: When you click this button, determine if there is a network, if still no network, pop-up toast prompts the user, if there is a network, remove this view, and let the agent execute the appropriate proxy method, for example, reload the data.
 /** Re-view button click */-(void) checknetworkbuttonclicked{if ([DateUtil Isnetworkrunning]) {//if there is a network, view disappears and let the agent execute the proxy method for ( Cqnonetworkview *view in [self Getcurrentviewcontroller]. View.subviews) {if ([View Ismemberofclass:[cqnonetworkview  Class]]) {[View Removefromsuperview];}} //Reload data if ([self.delegate Respondstoselector: @selector (reloaddata)]) {[ Self.delegate Reloaddata]; }}else{//if no net, toast hint [Cqhud showtoastwithmessage:@ "Please check your Internet connection"]; }}
    • To determine the proxy method (the method that the agent executes when the Reload button is clicked), my data is reloaded.
@protocol CheckNetworkDelegate <NSObject>@optional /** 重新加载数据 */ - (void)reloadData;@end /** 无网络时展示的view */@interface CQNoNetworkView : UIView@property (nonatomic,weak) id<CheckNetworkDelegate> delegate;@end
    • Identify the agent. The agent is the object that executes the reload data. Since each viewcontroller needs to show no net-occupied bitmaps at all, the agent can be set up as a project BaseViewController (don't tell me that there is no base class in your project??)
@interface BaseViewController ()<CheckNetworkDelegate>

Implementing reloading of data in a base class, of course, requires a subclass rewrite, but if your project schema is better, and all viewcontroller methods have the same method name, then you don't need to rewrite the subclass:

/** 重新加载数据,由子类重写 */     (void)reloadData{    }

Also in the base class to implement a pop-up non-net-occupied bitmap method:

/** 显示无网络view */- (void)showNoNetworkView{    // 将导航栏和tabbar留出来    CQNoNetworkView *noNetworkView = [[CQNoNetworkView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight - 49)]; noNetworkView.delegate = self; [self.view addSubview:noNetworkView];}
    • Determine the network situation in the base class viewWillAppear: :
// 如果没网,加载无网占位图    if (![DateUtil isNetWorkRunning]) {        // 无网加载无网站位图        [self showNoNetworkView];    }else{ // 有网移除所有无网展位图 for (CQNoNetworkView *view in self.view.subviews) { if ([view isMemberOfClass:[CQNoNetworkView class]]) { [view removeFromSuperview]; } } }
    • Finally, rewrite the "Reload data" method in your sub-class:
- (void)reloadData{    [self getData];}
Summarize

This function is mainly the use of agents and base classes, reasonable use of the base class can make you less a lot of code. Of course, some apps may not only have a network of bitmaps, this time in subclasses to rewrite the base class showNoNetworkView method. The above is my personal view of this function, but simply to achieve this function, the specific project will be based on the needs of the project to make the necessary changes and optimization. However, the idea is basically the same. Welcome all crossing to make comments or suggestions.

Add two more elegant and powerful demos

UITableView empty data for bitmaps
Generic placeholder bitmap for any scenario



The star without the night
Links: https://www.jianshu.com/p/d537393fe247
Source: Pinterest
The copyright of the book is owned by the author, and any form of reprint should be contacted by the author for authorization and attribution.

iOS Development--no net-occupied bitmap implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.