iOS开发中 new与alloc/init的区别 及 [NSArray array] 和 [[NSArray alloc]init] 及 self. 和 _ 的区别

本文详细解析Objective-C中new与alloc/init的区别,包括内存分配、初始化过程,以及如何正确使用避免内存泄漏。同时,介绍了self与_的区别、懒加载的概念,以及如何在开发中应用这些知识进行高效内存管理。

项目过程中,想到这几个概念的区别有些模糊,于是纵观各种资料,来篇博文为自己记录下,也为小伙伴们说说我的理解。

[className new] 和 [[className alloc] init] 的区别

1.在实际开发中很少会用到new,一般创建对象咱们看到的全是[[className alloc] init]

但是并不意味着你不会接触到new,在一些代码中还是会看到[[className alloc] init]

还有去面试的时候,也很可能被问到这个问题。


2.那么,他们两者之间到底有什么区别呢

我们看源码:

  1. new 
  2. id newObject = (*_alloc)((Class)self, 0); 
  3. Class metaClass = self->isa; 
  4. if (class_getVersion(metaClass) > 1) 
  5. return [newObject init]; 
  6. else 
  7. return newObject; 
  8.  
  9. //而 alloc/init 像这样: 
  10. + alloc 
  11. return (*_zoneAlloc)((Class)self, 0, malloc_default_zone());  
  12. - init 
  13. return self; 

通过源码中我们发现,[className new]基本等同于[[className alloc] init]

区别只在于alloc分配内存的时候使用了zone.

这个zone是个什么东东呢?

它是给对象分配内存的时候,把关联的对象分配到一个相邻的内存区域内,以便于调用时消耗很少的代价,提升了程序处理速度;

3.而为什么不推荐使用new?

不知大家发现了没有:如果使用new的话,初始化方法被固定死只能调用init.

而你想调用initXXX怎么办?没门儿!据说最初的设计是完全借鉴Smalltalk语法来的。

传说那个时候已经有allocFromZone:这个方法,

但是这个方法需要传个参数id myCompanion = [[TheClass allocFromZone:[self zone]] init];

这个方法像下面这样:

  1. + allocFromZone:(void *) z 
  2. return (*_zoneAlloc)((Class)self, 0, z);  
  3.  
  4. //后来简化为下面这个: 
  5. + alloc 
  6. return (*_zoneAlloc)((Class)self, 0, malloc_default_zone());  

但是,出现个问题:这个方法只是给对象分配了内存,并没有初始化实例变量。

是不是又回到new那样的处理方式:在方法内部隐式调用init方法呢?

后来发现“显示调用总比隐式调用要好”,所以后来就把两个方法分开了。

概括来说,new和alloc/init在功能上几乎是一致的,分配内存并完成初始化。

差别在于,采用new的方式只能采用默认的init方法完成初始化,

采用alloc的方式可以用其他定制的初始化方法。



再来说一个突然想起来的问题:[NSArray array] [[NSArray alloc]init] (包括字典等同类) 的使用方法的区别:


alloc (内存分配)以及init(初始化) Objective-C 协议分为非正式协议和正式协议 .

这两个方式都是建立一个空的Array
[NSArray array]不需要release,使用autoreleasepool机制。
[[NSArray alloc] init]需要自己手动release


项目使用崩溃实例:


在ViewDidLoad中   
jsonDataDic = [NSMutableDictionary dictionary];    
[self jsonParse];
创建一个空字典,在jsonParse中使用了这个词典,导致程序崩溃
解决方法:在jsonDataDic前面加上self.即可
原因:不加的话,指针的作用域仅在ViewDidLoad中,进入jsonParse后该指针已释放,成为了一个野指针,再对其进行操作,使程序崩溃。
注意:字典是没有顺序的,字典的allkeys或者allvalues存放到数组中是随机的。

总结:


new做的事情和alloc init是一样的,当然你要构造方法是init的时候完全可以用new来代替 ,alloc 不仅仅可以使用init构造方法,更可以自定义构造方法e.g initWithFrame等等。
另外,alloc开辟空间后能够自动清空新开辟内存空间中的老数据,不会出现莫名奇妙的错误,见《Objective-C基础教程》


说到这个问题,可能有小伙伴又懵了,那 self. _ 有什么区别呢,再说下这两个 :

( - -!没完了)


self.programStack等于[self programStack],会走你的懒加载方法;而_programStack类似于self->_programStack。

用self点出属性是更好的选择,因为这样可以兼容懒加载,同时也避免了使用下划线的时候忽视了self这个指针,后者容易在block中造成循环引用。


科普:懒加载(Load On Demand)是一种独特而又强大的数据获取方法,它能够在用户滚动页面的时候自动获取更多的数据,而新得到的数据不会影响原有数据的显示,同时最大程度上减少服务器端的资源耗用。


这两种使用方式显然是不一样的。


主要是涉及到内存管理的问题。self.propertyName 使用self. 是对属性的访问。使用_ 是对局部变量的访问。
所有被声明为属性的成员,在ios5 之前需要使用编译器指令@synthesize 来告诉编译器帮助生成属性的getter,setter方法。之后这个指令可以不用人为指定了,默认情况下编译器会帮我们生成。 编译器在生成getter,setter方法时是有优先级的,它首先查找当前的类中用户是否已定义属性的getter,setter方法,如果有,则编译器会跳过,不会再生成,使用用户定义的方法。 也就是说你在使用self.propertyName 时是在调用一个方法。


_______________________________________________________哥哥爱开发_________________________________________________________________

本篇博文有查询资料中大神的理论引用,有自己的原创理解,只为整理在一起方便理解。

最后:开发,只为简单生活,技术改变世界!~yeah~

分析一下下面的代码有几个触发事件 // // SDNGlobalOverviewInfoView.m // Omada // // Created by qijiayue on 2022/11/24. // Copyright © 2022 TP-Link. All rights reserved. // #import "SDNGlobalOverviewInfoView.h" #import "SDNDashboardOverviewItemView.h" #import "SDNDashboardCloudAccessTitleView.h" #import "VMSDNGlobalOverviewInfo.h" #import "TPTitleAlertView.h" @interface SDNGlobalOverviewInfoView() @property (nonatomic, strong) SDNDashboardOverviewItemView *gatewayItemView; @property (nonatomic, strong) SDNDashboardOverviewItemView *switchesItemView; @property (nonatomic, strong) SDNDashboardOverviewItemView *eapsItemView; @property (nonatomic, strong) SDNDashboardOverviewItemView *clientsItemView; @property (nonatomic, strong) SDNDashboardOverviewItemView *AlertItemView; @property (nonatomic, strong) SDNDashboardOverviewItemView *siteItemView; @property (nonatomic, strong) UIImageView *dropDownImgView; @property (nonatomic, strong) TPBlockControl *dropDownControl; @property (nonatomic, strong) UIView *cloudAccessView; @property (nonatomic, strong) UILabel *accessLabel; @property (nonatomic, strong) UILabel *accessDetailLabel; @property (nonatomic, strong) VMSDNGlobalOverviewInfo *overviewInfo; @property (nonatomic, strong) UIView *overViewDetailView; @property (nonatomic, strong) MASConstraint *cloudAccessViewBottomConstraint; @property (nonatomic, strong) MASConstraint *itemListViewBottomConstraint; @property (nonatomic, assign) BOOL showItemListView; @property (nonatomic, assign) BOOL hideClientView; @property (nonatomic, assign) DMSDNGlobalOverviewInfo *dmOverViewInfo; @end @implementation SDNGlobalOverviewInfoView - (instancetype)initWithHideClientView:(BOOL)hideClientView { self = [super init]; if (self) { self.showItemListView = YES; self.hideClientView = hideClientView; [self buildView]; // TPWeakSelf // [self tpbAddContentSizeDidChangeConfig:^(id _Nonnull object, TPBDynamicContentManager * _Nonnull manager) { // TPBStrongSelf // dispatch_async(dispatch_get_main_queue(), ^{ // [_self buildConstraint]; // [_self needsUpdateConstraints]; // }); // } notifyWhenRegister:NO]; } return self; } - (void)updateWithDynamicFont { for (UIView *view in self.overViewDetailView.subviews) { if ([view isKindOfClass:SDNDashboardOverviewItemView.class]) { SDNDashboardOverviewItemView *itemView = (SDNDashboardOverviewItemView *)view; [itemView updateWithDynamicFont]; } } [self buildConstraint]; } - (NSArray *)accessibilityElements{ NSMutableArray *array = [NSMutableArray new]; if (!self.cloudAccessView.hidden) { [array addObject:self.cloudAccessView]; } if (!self.dropDownImgView.hidden) { [array addObject:self.dropDownImgView]; } if (!self.overViewDetailView.hidden) { [array addObject:self.overViewDetailView]; } if (!self.cloudAccessView.hidden) { [array addObject:self.cloudAccessView]; } if (!self.siteItemView.hidden) { [array addObject:self.siteItemView]; } if (!self.gatewayItemView.hidden) { [array addObject:self.gatewayItemView]; } if (!self.switchesItemView.hidden) { [array addObject:self.switchesItemView]; } if (!self.eapsItemView.hidden) { [array addObject:self.eapsItemView]; } if (!self.clientsItemView.hidden) { [array addObject:self.clientsItemView]; } if (!self.AlertItemView.hidden) { [array addObject:self.AlertItemView]; } return [array copy]; } - (BOOL)isAccessibilityElement { return NO; } - (void)layoutSubviews { [super layoutSubviews]; self.dropDownImgView.accessibilityFrame = [TPBA11yHelper getEnlargedAccessibilityFrame:self.dropDownImgView]; } - (void)buildView { // CGFloat overItemViewWidth = (kScreen_Width - 40)/3; //GlobalOverviewInfoView卡片其他卡片颜色保持一致 self.backgroundColor = [UIColor tpbCard]; self.accessLabel = [[UILabel alloc] init]; self.accessLabel.tpbDynamicFont = [UIFont tpr18Regular]; self.accessLabel.textColor = [UIColor tpbTextPrimary]; self.accessLabel.numberOfLines = 1; self.accessLabel.text = gSDNQuickSetup.cloudAccessTitle; [self.accessLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical]; [self.cloudAccessView addSubview:self.accessLabel]; // [self.accessLabel mas_makeConstraints:^(MASConstraintMaker *make) { // make.leading.equalTo(self.cloudAccessView).offset(12); // make.top.equalTo(self.cloudAccessView).offset(10); // make.bottom.equalTo(self.cloudAccessView).offset(-8); // }]; self.accessDetailLabel = [[UILabel alloc] init]; self.accessDetailLabel.tpbDynamicFont = [UIFont tpr14Regular]; [self.accessDetailLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical]; self.accessDetailLabel.numberOfLines = 1; self.accessDetailLabel.text = @" "; [self.cloudAccessView addSubview:self.accessDetailLabel]; // [self.accessDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) { // make.centerY.equalTo(self.accessLabel); // make.trailing.equalTo(self.cloudAccessView).offset(-12); // }]; [self addSubview:self.cloudAccessView]; [self addSubview:self.dropDownImgView]; // [self.cloudAccessView mas_makeConstraints:^(MASConstraintMaker *make) { // TPStrongSelf; // make.top.equalTo(_self).offset(12); // make.leading.equalTo(_self); // make.trailing.equalTo(_self.dropDownImgView.mas_leading); // _self.cloudAccessViewBottomConstraint = make.bottom.equalTo(_self).offset(-8); // }]; // [self.dropDownImgView mas_makeConstraints:^(MASConstraintMaker *make) { // TPStrongSelf; //// make.top.equalTo(_self).offset(12); // make.centerY.equalTo(_self.accessLabel); // make.trailing.equalTo(_self).offset(-10); // make.height.width.equalTo(@(24)); // }]; // [self.cloudAccessViewBottomConstraint uninstall]; [self addSubview:self.overViewDetailView]; // [self.overViewDetailView mas_makeConstraints:^(MASConstraintMaker *make) { // TPStrongSelf; // make.top.equalTo(_self.cloudAccessView.mas_bottom); // make.trailing.leading.equalTo(_self); // self.itemListViewBottomConstraint = make.bottom.equalTo(_self); // }]; [self.overViewDetailView addSubview:self.siteItemView]; // [self.siteItemView mas_makeConstraints:^(MASConstraintMaker *make) { // TPStrongSelf; // make.top.equalTo(_self.overViewDetailView).offset(8); // make.leading.equalTo(_self.overViewDetailView); //// make.width.lessThanOrEqualTo(@(overItemViewWidth)); // }]; [self.overViewDetailView addSubview:self.gatewayItemView]; // [self.gatewayItemView mas_makeConstraints:^(MASConstraintMaker *make) { // TPStrongSelf; //// make.centerX.equalTo(_self.overViewDetailView); // make.top.equalTo(_self.siteItemView); //// make.width.lessThanOrEqualTo(@(overItemViewWidth)); // make.leading.equalTo(self.siteItemView.mas_trailing); // make.width.equalTo(self.siteItemView); // }]; [self.overViewDetailView addSubview:self.switchesItemView]; // [self.switchesItemView mas_makeConstraints:^(MASConstraintMaker *make) { // TPStrongSelf; // make.trailing.equalTo(_self.overViewDetailView); // make.top.equalTo(_self.siteItemView); //// make.width.lessThanOrEqualTo(@(overItemViewWidth)); // make.leading.equalTo(self.gatewayItemView.mas_trailing); // make.width.equalTo(self.siteItemView); // }]; [self.overViewDetailView addSubview:self.eapsItemView]; // [self.eapsItemView mas_makeConstraints:^(MASConstraintMaker *make) { // TPStrongSelf; // make.centerX.width.equalTo(_self.siteItemView); // make.top.equalTo(_self.siteItemView.mas_bottom).offset(20); // make.bottom.equalTo(_self.overViewDetailView).offset(-20); //// make.width.lessThanOrEqualTo(@(overItemViewWidth)); // }]; [self.overViewDetailView addSubview:self.clientsItemView]; [self.overViewDetailView addSubview:self.AlertItemView]; self.clientsItemView.hidden = self.hideClientView; // if (self.hideClientView) // { // // [self.overViewDetailView addSubview:self.clientsItemView]; // self.clientsItemView.hidden = YES; // // [self.overViewDetailView addSubview:self.AlertItemView]; // [self.AlertItemView mas_makeConstraints:^(MASConstraintMaker *make) { // TPStrongSelf; // make.centerX.width.equalTo(_self.gatewayItemView); // make.top.equalTo(_self.eapsItemView); // make.bottom.equalTo(_self.eapsItemView); //// make.width.lessThanOrEqualTo(@(overItemViewWidth)); // }]; // } // else // { // // [self.overViewDetailView addSubview:self.clientsItemView]; // [self.clientsItemView mas_makeConstraints:^(MASConstraintMaker *make) { // TPStrongSelf; // make.centerX.width.equalTo(_self.gatewayItemView); // make.top.equalTo(_self.eapsItemView); // make.bottom.equalTo(_self.eapsItemView); //// make.width.lessThanOrEqualTo(@(overItemViewWidth)); // }]; // // [self.overViewDetailView addSubview:self.AlertItemView]; // [self.AlertItemView mas_makeConstraints:^(MASConstraintMaker *make) { // TPStrongSelf; // make.centerX.width.equalTo(_self.switchesItemView); // make.top.equalTo(_self.eapsItemView); // make.bottom.equalTo(_self.eapsItemView); //// make.width.lessThanOrEqualTo(@(overItemViewWidth)); // }]; // } [self buildConstraint]; NSMutableArray *array = [NSMutableArray new]; if (!self.siteItemView.hidden) { [array addObject:self.siteItemView]; } if (!self.gatewayItemView.hidden) { [array addObject:self.gatewayItemView]; } if (!self.switchesItemView.hidden) { [array addObject:self.switchesItemView]; } if (!self.eapsItemView.hidden) { [array addObject:self.eapsItemView]; } if (!self.clientsItemView.hidden) { [array addObject:self.clientsItemView]; } if (!self.AlertItemView.hidden) { [array addObject:self.AlertItemView]; } self.overViewDetailView.accessibilityElements = [array copy]; } - (void)buildConstraint { if ([TPBDynamicContentManager sharedManager].isLargeContent) { [self updateConstraintsWithLargeContent]; } else { [self updateConstraintsNormal]; } } - (void)updateConstraintsNormal { [self.accessLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.leading.equalTo(self.cloudAccessView).offset(12); make.top.equalTo(self.cloudAccessView).offset(10); make.bottom.equalTo(self.cloudAccessView).offset(-8); }]; [self.accessDetailLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self.accessLabel); make.trailing.equalTo(self.cloudAccessView).offset(-12); }]; [self.dropDownImgView mas_remakeConstraints:^(MASConstraintMaker *make) { // make.top.equalTo(_self).offset(12); make.centerY.equalTo(self.accessLabel); make.trailing.equalTo(self).offset(-10); make.height.width.equalTo(@(24)); }]; [self.cloudAccessView mas_remakeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self).offset(12); make.leading.equalTo(self); make.trailing.equalTo(self.dropDownImgView.mas_leading); if (!self.showItemListView) { make.bottom.equalTo(self).offset(-12); } }]; [self.overViewDetailView mas_remakeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.cloudAccessView.mas_bottom); make.trailing.leading.equalTo(self); if (self.showItemListView) { make.bottom.equalTo(self); } }]; [self.siteItemView mas_remakeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.overViewDetailView).offset(8); make.leading.equalTo(self.overViewDetailView); }]; [self.gatewayItemView mas_remakeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.siteItemView); make.leading.equalTo(self.siteItemView.mas_trailing); make.width.equalTo(self.siteItemView); }]; [self.switchesItemView mas_remakeConstraints:^(MASConstraintMaker *make) { make.trailing.equalTo(self.overViewDetailView); make.top.equalTo(self.siteItemView); make.leading.equalTo(self.gatewayItemView.mas_trailing); make.width.equalTo(self.siteItemView); }]; [self.eapsItemView mas_remakeConstraints:^(MASConstraintMaker *make) { make.centerX.width.equalTo(self.siteItemView); make.top.equalTo(self.siteItemView.mas_bottom).offset(20); make.bottom.equalTo(self.overViewDetailView).offset(-20); }]; self.clientsItemView.hidden = self.hideClientView; [self.clientsItemView mas_remakeConstraints:^(MASConstraintMaker *make) { make.centerX.width.equalTo(self.gatewayItemView); make.top.equalTo(self.eapsItemView); make.bottom.equalTo(self.eapsItemView); }]; [self.AlertItemView mas_remakeConstraints:^(MASConstraintMaker *make) { if (self.hideClientView) { make.centerX.width.equalTo(self.gatewayItemView); } else { make.centerX.width.equalTo(self.switchesItemView); } make.top.equalTo(self.eapsItemView); make.bottom.equalTo(self.eapsItemView); }]; } - (void)updateConstraintsWithLargeContent { [self.accessLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.leading.equalTo(self.cloudAccessView).offset(12); make.top.equalTo(self.cloudAccessView).offset(10); }]; [self.accessDetailLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.accessLabel.mas_bottom).offset(8); make.leading.equalTo(self.accessLabel); make.bottom.equalTo(self.cloudAccessView).offset(-8); }]; [self.dropDownImgView mas_remakeConstraints:^(MASConstraintMaker *make) { // make.top.equalTo(_self).offset(12); make.centerY.equalTo(self.accessLabel); make.trailing.equalTo(self).offset(-10); make.height.width.equalTo(@(24)); }]; [self.cloudAccessView mas_remakeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self).offset(12); make.leading.equalTo(self); make.trailing.equalTo(self.dropDownImgView.mas_leading); if (!self.showItemListView) { make.bottom.equalTo(self).offset(-8); } }]; [self.overViewDetailView mas_remakeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.cloudAccessView.mas_bottom); make.trailing.leading.equalTo(self); if (self.showItemListView) { make.bottom.equalTo(self); } }]; [self.siteItemView mas_remakeConstraints:^(MASConstraintMaker *make) { make.leading.trailing.equalTo(self.overViewDetailView); make.top.equalTo(self.overViewDetailView).offset(12); }]; [self.gatewayItemView mas_remakeConstraints:^(MASConstraintMaker *make) { make.leading.trailing.equalTo(self.overViewDetailView); make.top.equalTo(self.siteItemView.mas_bottom).offset(4); }]; [self.switchesItemView mas_remakeConstraints:^(MASConstraintMaker *make) { make.leading.trailing.equalTo(self.overViewDetailView); make.top.equalTo(self.gatewayItemView.mas_bottom).offset(4); }]; [self.eapsItemView mas_remakeConstraints:^(MASConstraintMaker *make) { make.leading.trailing.equalTo(self.overViewDetailView); make.top.equalTo(self.switchesItemView.mas_bottom).offset(4); }]; self.clientsItemView.hidden = self.hideClientView; [self.clientsItemView mas_remakeConstraints:^(MASConstraintMaker *make) { make.leading.trailing.equalTo(self.overViewDetailView); make.top.equalTo(self.eapsItemView.mas_bottom).offset(4); }]; [self.AlertItemView mas_remakeConstraints:^(MASConstraintMaker *make) { make.leading.trailing.equalTo(self.overViewDetailView); if (self.hideClientView) { make.top.equalTo(self.eapsItemView.mas_bottom).offset(4); } else { make.top.equalTo(self.clientsItemView.mas_bottom).offset(4); } make.bottom.equalTo(self.overViewDetailView).offset(-16); }]; } - (void)updateWithOverviewInfo:(DMSDNGlobalOverviewInfo *)overViewInfo andTotalAlertInfo:(DMSDNGlobalSiteAlertInfo *)alertInfo { self.dmOverViewInfo = overViewInfo; VMSDNGlobalOverviewInfo *info = [[VMSDNGlobalOverviewInfo alloc] init]; [info updateWithOverviewInfo:overViewInfo andTotalAlertInfo:alertInfo]; self.overviewInfo = info; [self updateViewWithCloudAccessStatus:self.overviewInfo.status]; // [self.cloudAccessView updateViewForGlobalDashboardView]; if (self.showItemListView) { [self.siteItemView updateViewWithDetail:self.overviewInfo.totalSiteNum]; [_siteItemView updateViewWithImgName:@"SDNDashboardSite" andTitle:gControllerSites.controllerSitesTitle]; self.siteItemView.customLabel = [gAccessibility.globalSite replaceFormatPlaceHolders:@[self.overviewInfo.totalSiteNum]]; [self.AlertItemView updateViewWithDetail:self.overviewInfo.totalAlertNum]; [_AlertItemView updateViewWithImgName:@"SDNDashboardAlerts" andTitle:gSDNDashboard.sdnDashboardAlerts]; self.AlertItemView.customLabel = [gAccessibility.globalAlerts replaceFormatPlaceHolders:@[self.overviewInfo.totalAlertNum]]; [self.clientsItemView updateViewWithDetail:self.overviewInfo.totalClientNum]; [_clientsItemView updateViewWithImgName:@"SDNDashboardClient" andTitle:gSDNGlobal.clients]; self.clientsItemView.customLabel = [TPBA11yHelper combineAccessibilityTextArray:@[gAccessibility.globalClients, self.overviewInfo.totalAlertNum]]; [self.gatewayItemView updateViewWithDetail:self.overviewInfo.gatewayNum]; [_gatewayItemView updateViewWithImgName:@"SDNDashboardGateway" andTitle:gMeshQuickSetup.controllerGateways]; self.gatewayItemView.customLabel = [gAccessibility.globalGateway replaceFormatPlaceHolders:@[[self getDeviceNumString:(int)overViewInfo.connectedGatewayNum], [self getDeviceNumString:(int)overViewInfo.totalGatewayNum]]]; [self.switchesItemView updateViewWithDetail:self.overviewInfo.switchNum]; [_switchesItemView updateViewWithImgName:@"SDNDashboardSwitch" andTitle:gSDNDashboard.sdnDashboardSwitches]; self.switchesItemView.customLabel = [gAccessibility.globalSwitches replaceFormatPlaceHolders:@[[self getDeviceNumString:(int)overViewInfo.connectedSwitchNum], [self getDeviceNumString:(int)overViewInfo.totalSwitchNum]]]; [self.eapsItemView updateViewWithDetail:self.overviewInfo.apNum]; [_eapsItemView updateViewWithImgName:@"SDNDashboardAp" andTitle:gControllerStatistics.controllerStGlobalStatCardTitleAPs]; self.eapsItemView.customLabel = [gAccessibility.globalEAPs replaceFormatPlaceHolders:@[[self getDeviceNumString:(int)overViewInfo.connectedApNum], [self getDeviceNumString:(int)overViewInfo.totalApNum]]]; if (overViewInfo.connectedApNum > 0) { [self.eapsItemView updateViewWithDetailForGreen:self.overviewInfo.apNum]; } else { [self.eapsItemView updateViewWithDetailForRed:self.overviewInfo.apNum]; } if (overViewInfo.connectedSwitchNum > 0) { [self.switchesItemView updateViewWithDetailForGreen:self.overviewInfo.switchNum]; } else { [self.switchesItemView updateViewWithDetailForRed:self.overviewInfo.switchNum]; } if (overViewInfo.connectedGatewayNum > 0) { [self.gatewayItemView updateViewWithDetailForGreen:self.overviewInfo.gatewayNum]; } else { [self.gatewayItemView updateViewWithDetailForRed:self.overviewInfo.gatewayNum]; } } } - (NSString *)getDeviceNumString:(int)number { return [NSString stringWithFormat:@"%d", number]; } - (void)updateWithOverviewInfo:(DMSDNGlobalOverviewInfo *)overViewInfo andTotalAlertInfo:(DMSDNGlobalSiteAlertInfo *)alertInfo andClientOverviewInfo:(DMSDNClientOverviewInfo *)clientOverviewInfo { VMSDNGlobalOverviewInfo *info = [[VMSDNGlobalOverviewInfo alloc] init]; [info updateWithOverviewInfo:overViewInfo andTotalAlertInfo:alertInfo andClientOverviewInfo:clientOverviewInfo]; self.overviewInfo = info; [self updateViewWithCloudAccessStatus:self.overviewInfo.status]; // [self.cloudAccessView updateViewForGlobalDashboardView]; if (self.showItemListView) { [self.siteItemView updateViewWithDetail:self.overviewInfo.totalSiteNum]; [_siteItemView updateViewWithImgName:@"SDNDashboardSite" andTitle:gControllerSites.controllerSitesTitle]; [self.AlertItemView updateViewWithDetail:self.overviewInfo.totalAlertNum]; [_AlertItemView updateViewWithImgName:@"SDNDashboardAlerts" andTitle:gSDNDashboard.sdnDashboardAlerts]; [self.clientsItemView updateViewWithDetail:self.overviewInfo.totalClientNum]; [_clientsItemView updateViewWithImgName:@"SDNDashboardClient" andTitle:gSDNGlobal.clients]; [self.gatewayItemView updateViewWithDetail:self.overviewInfo.gatewayNum]; [_gatewayItemView updateViewWithImgName:@"SDNDashboardGateway" andTitle:gMeshQuickSetup.controllerGateways]; [self.switchesItemView updateViewWithDetail:self.overviewInfo.switchNum]; [_switchesItemView updateViewWithImgName:@"SDNDashboardSwitch" andTitle:gSDNDashboard.sdnDashboardSwitches]; [self.eapsItemView updateViewWithDetail:self.overviewInfo.apNum]; [_eapsItemView updateViewWithImgName:@"SDNDashboardAp" andTitle:gControllerStatistics.controllerStGlobalStatCardTitleAPs]; self.siteItemView.customLabel = [gAccessibility.globalSite replaceFormatPlaceHolders:@[self.overviewInfo.totalSiteNum]]; self.AlertItemView.customLabel = [gAccessibility.globalAlerts replaceFormatPlaceHolders:@[self.overviewInfo.totalAlertNum]]; self.clientsItemView.customLabel = [TPBA11yHelper combineAccessibilityTextArray:@[gAccessibility.globalClients, self.overviewInfo.totalAlertNum]]; self.gatewayItemView.customLabel = [gAccessibility.globalGateway replaceFormatPlaceHolders:@[[self getDeviceNumString:(int)overViewInfo.connectedGatewayNum], [self getDeviceNumString:(int)overViewInfo.totalGatewayNum]]]; self.switchesItemView.customLabel = [gAccessibility.globalSwitches replaceFormatPlaceHolders:@[[self getDeviceNumString:(int)overViewInfo.connectedSwitchNum], [self getDeviceNumString:(int)overViewInfo.totalSwitchNum]]]; self.eapsItemView.customLabel = [gAccessibility.globalEAPs replaceFormatPlaceHolders:@[[self getDeviceNumString:(int)overViewInfo.connectedApNum], [self getDeviceNumString:(int)overViewInfo.totalApNum]]]; if (overViewInfo.connectedApNum > 0) { [self.eapsItemView updateViewWithDetailForGreen:self.overviewInfo.apNum]; } else { [self.eapsItemView updateViewWithDetailForRed:self.overviewInfo.apNum]; } if (overViewInfo.connectedSwitchNum > 0) { [self.switchesItemView updateViewWithDetailForGreen:self.overviewInfo.switchNum]; } else { [self.switchesItemView updateViewWithDetailForRed:self.overviewInfo.switchNum]; } if (overViewInfo.connectedGatewayNum > 0) { [self.gatewayItemView updateViewWithDetailForGreen:self.overviewInfo.gatewayNum]; } else { [self.gatewayItemView updateViewWithDetailForRed:self.overviewInfo.gatewayNum]; } } } - (void)didClickButton { self.showItemListView = !self.showItemListView; if (self.clickBotton) { self.clickBotton(self.showItemListView); } [self updateListView:self.showItemListView]; [self updateViewWithCloudAccessStatus:self.overviewInfo.status]; // [self.cloudAccessView updateViewForGlobalDashboardView]; } - (void)updateListView:(BOOL)isShowItemListView { self.showItemListView = isShowItemListView; if (isShowItemListView) { self.dropDownImgView.image = [[UIImage imageNamed:@"SDNArrowUp"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; // [self.cloudAccessViewBottomConstraint uninstall]; // [self.itemListViewBottomConstraint install]; } else { // [self.itemListViewBottomConstraint uninstall]; // [self.cloudAccessViewBottomConstraint install]; self.dropDownImgView.image = [[UIImage imageNamed:@"SDNArrowDown"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; } [self buildConstraint]; } //MARK: -lazy - (UIView *)cloudAccessView { if (_cloudAccessView == nil) { _cloudAccessView = [[UIView alloc] init]; // [_cloudAccessView setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical]; // [_cloudAccessView updateViewForGlobalDashboardView]; } return _cloudAccessView; } - (UIView *)overViewDetailView { if (_overViewDetailView == nil) { _overViewDetailView = [[UIView alloc] init]; [_overViewDetailView setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical]; _overViewDetailView.isAccessibilityElement = NO; } return _overViewDetailView; } - (UIImageView *)dropDownImgView { if (_dropDownImgView == nil) { _dropDownImgView = [[UIImageView alloc] init]; _dropDownImgView.image = [[UIImage imageNamed:@"SDNArrowUp"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; _dropDownImgView.tintColor = [UIColor tpbTextThirdContent]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickButton)]; [_dropDownImgView addGestureRecognizer:tap]; _dropDownImgView.userInteractionEnabled = YES; _dropDownImgView.isAccessibilityElement = YES; _dropDownImgView.accessibilityLabel = gAccessibility.collapse; _dropDownImgView.accessibilityTraits = UIAccessibilityTraitButton; } return _dropDownImgView; } - (SDNDashboardOverviewItemView *)siteItemView { if (_siteItemView == nil) { _siteItemView = [[SDNDashboardOverviewItemView alloc] init]; [_siteItemView updateViewWithImgName:@"dashboardSite" andTitle:gControllerSites.controllerSitesTitle]; [_siteItemView updateViewWithDetail:@"0"]; } return _siteItemView; } - (SDNDashboardOverviewItemView *)gatewayItemView { if (_gatewayItemView == nil) { _gatewayItemView = [[SDNDashboardOverviewItemView alloc] init]; [_gatewayItemView updateViewWithImgName:@"dashboardGateway" andTitle:gControllerSettings.controllerSettingsComponentNetworkSettingTextGateway]; [_gatewayItemView updateViewWithDetail:gSDNDashboard.sdnDashboardOverviewDiagramNone]; } return _gatewayItemView; } - (SDNDashboardOverviewItemView *)switchesItemView { if (_switchesItemView == nil) { _switchesItemView = [[SDNDashboardOverviewItemView alloc] init]; [_switchesItemView updateViewWithImgName:@"dashboardSwitch" andTitle:gSDNDashboard.sdnDashboardSwitches]; [_switchesItemView updateViewWithDetail:gSDNDashboard.sdnDashboardOverviewDiagramNone]; } return _switchesItemView; } - (SDNDashboardOverviewItemView *)eapsItemView { if (_eapsItemView == nil) { _eapsItemView = [[SDNDashboardOverviewItemView alloc] init]; [_eapsItemView updateViewWithImgName:@"dashboardAP" andTitle:gSDNDashboard.sdnDashboardEaps]; [_eapsItemView updateViewWithDetail:gSDNDashboard.sdnDashboardOverviewDiagramNone]; } return _eapsItemView; } - (SDNDashboardOverviewItemView *)clientsItemView { if (_clientsItemView == nil) { _clientsItemView = [[SDNDashboardOverviewItemView alloc] init]; [_clientsItemView updateViewWithImgName:@"dashboardClient" andTitle:gSDNGlobal.clients]; [_clientsItemView updateViewWithDetail:@"0"]; } return _clientsItemView; } - (SDNDashboardOverviewItemView *)AlertItemView { if (_AlertItemView == nil) { _AlertItemView = [[SDNDashboardOverviewItemView alloc] init]; [_AlertItemView updateViewWithImgName:@"dashBoardAlert" andTitle:gMeshQuickSetup.sdnGlobalDashboardOverviewAlertTitle]; [_AlertItemView updateViewWithDetail:@"0"]; if ([TPSDNControllerClient currentInstance].globalLogComponent.isSupported) { [_AlertItemView updateAttentionButtonHidden:YES]; } else { [_AlertItemView updateAttentionButtonHidden:NO]; } _AlertItemView.didClickButton = ^{ TPTitleAlertView *alertView = [[TPTitleAlertView alloc] init]; [alertView updateTitle:gMeshQuickSetup.goToWebToViewAlert]; alertView.buttonTitleArray = @[gSDNGlobal.ok]; [alertView show]; }; } return _AlertItemView; } - (void)updateViewWithCloudAccessStatus:(DMSDNDashboardCloudAccessStatus)cloudAccessStatus { NSString *str = @""; UIColor *color = [UIColor clearColor]; switch (cloudAccessStatus) { case DMSDNDashboardCloudAccessStatusConnected: { str = gSDNGlobal.connected; color = [UIColor tpbGreen]; break; } case DMSDNDashboardCloudAccessStatusDisconnected: { str = gSDNGlobal.disconnected; color = [UIColor tpbRed]; break; } case DMSDNDashboardCloudAccessStatusDisabled: { str = gSDNGlobal.disabled; color = [UIColor tpbTextDisabled]; break; } default: break; } // self.statusImgView.backgroundColor = color; self.accessDetailLabel.textColor = color; self.accessDetailLabel.text = str; } - (void)dealloc { DDLogDebug(@"SDNGlobalOverviewInfoView dealloc"); } @end
11-26
iOS_Training_Demo1 /Users/yaohemoshuile/Downloads/iOS新人学习整理/Training1/iOS_Training_Demo1/iOS_Training_Demo1/Train1ViewController.m /Users/yaohemoshuile/Downloads/iOS新人学习整理/Training1/iOS_Training_Demo1/iOS_Training_Demo1/Train1ViewController.m:446:10 Property 'tableView' not found on object of type 'Train1ViewController *' /Users/yaohemoshuile/Downloads/iOS新人学习整理/Training1/iOS_Training_Demo1/iOS_Training_Demo1/Train1ViewController.m:447:10 Property 'tableView' not found on object of type 'Train1ViewController *' /Users/yaohemoshuile/Downloads/iOS新人学习整理/Training1/iOS_Training_Demo1/iOS_Training_Demo1/Train1ViewController.m:450:68 No visible @interface for 'MJRefreshNormalHeader' declares the selector 'initWithRefreshingBlock:' /Users/yaohemoshuile/Downloads/iOS新人学习整理/Training1/iOS_Training_Demo1/iOS_Training_Demo1/Train1ViewController.m:453:11 Property 'tableView' not found on object of type 'Train1ViewController *' /Users/yaohemoshuile/Downloads/iOS新人学习整理/Training1/iOS_Training_Demo1/iOS_Training_Demo1/Train1ViewController.m:454:10 Property 'tableView' not found on object of type 'Train1ViewController *' /Users/yaohemoshuile/Downloads/iOS新人学习整理/Training1/iOS_Training_Demo1/iOS_Training_Demo1/Train1ViewController.m:457:76 No visible @interface for 'MJRefreshAutoNormalFooter' declares the selector 'initWithRefreshingBlock:' /Users/yaohemoshuile/Downloads/iOS新人学习整理/Training1/iOS_Training_Demo1/iOS_Training_Demo1/Train1ViewController.m:460:11 Property 'tableView' not found on object of type 'Train1ViewController *' /Users/yaohemoshuile/Downloads/iOS新人学习整理/Training1/iOS_Training_Demo1/iOS_Training_Demo1/Train1ViewController.m:461:10 Property 'tableView' not found on object of type 'Train1ViewController *' /Users/yaohemoshuile/Downloads/iOS新人学习整理/Training1/iOS_Training_Demo1/iOS_Training_Demo1/Train1ViewController.m:472:15 Property 'tableView' not found on object of type 'Train1ViewController *' /Users/yaohemoshuile/Downloads/iOS新人学习整理/Training1/iOS_Training_Demo1/iOS_Training_Demo1/Train1ViewController.m:474:19 Property 'tableView' not found on object of type 'Train1ViewController *' /Users/yaohemoshuile/Downloads/iOS新人学习整理/Training1/iOS_Training_Demo1/iOS_Training_Demo1/Train1ViewController.m:474:48 Use of undeclared identifier 'MJRefreshFooterStateNoMoreData'; did you mean 'MJRefreshStateNoMoreData'? /Users/yaohemoshuile/Downloads/iOS新人学习整理/Training1/iOS_Training_Demo1/iOS_Training_Demo1/Train1ViewController.m:476:19 Property 'tableView' not found on object of type 'Train1ViewController *' /Users/yaohemoshuile/Downloads/iOS新人学习整理/Training1/iOS_Training_Demo1/iOS_Training_Demo1/Train1ViewController.m:484:15 Property 'tableView' not found on object of type 'Train1ViewController *' /Users/yaohemoshuile/Downloads/iOS新人学习整理/Training1/iOS_Training_Demo1/iOS_Training_Demo1/Train1ViewController.m:492:19 Property 'tableView' not found on object of type 'Train1ViewController *' /Users/yaohemoshuile/Downloads/iOS新人学习整理/Training1/iOS_Training_Demo1/iOS_Training_Demo1/Train1ViewController.m:494:19 Property 'tableView' not found on object of type 'Train1ViewController *' /Users/yaohemoshuile/Downloads/iOS新人学习整理/Training1/iOS_Training_Demo1/iOS_Training_Demo1/Train1ViewController.m:527:19 Property 'tableView' not found on object of type 'Train1ViewController *'
05-28
快速分析页面代码 // // SDNWANSettingVC.m // Omada // // Created by qijiayue on 2022/12/19. // Copyright © 2022 TP-Link. All rights reserved. // #import "SDNWANSettingVC.h" #import "SDNWANSettingVCDataSource.h" #import "ALSDNSiteInternetSetting.h" #import "TPAlertViewFactory.h" #import "TPTextAlertView.h" #import "SDNPermissionTool.h" #import "TPListSelectIndexVC.h" #import "TPSDNControllerCommonMethod.h" #import "SLRegionISPInfo.h" #import "SLDSLDataModelTransformMethods.h" #import "SLDSLSelectISPViewController.h" #import "SLSelectDSLMudoViewController.h" #import "SDNQSSelectCountryTableViewController.h" #import "TPSLGatewayWanSettingAbstractLayer.h" #import "TPSimpleCountryTool.h" #import "TPDslIspTool.h" #import "TPBCommonTableHelper.h" #import "OMDListSelectPopUpView.h" #import "SDNV6PermissionTool.h" NSString *ipv4ConnectionTypeFlag = @"ipv4ConnectionTypeFlag"; NSString *ipv6ConnectionTypeFlag = @"ipv6ConnectionTypeFlag"; NSString *ipv4InternetVlanPriorityFlag = @"ipv4InternetVlanPriorityFlag"; NSString *ipv6GetAddressMethodFlag = @"ipv6GetAddressMethodFlag"; @interface SDNWANSettingVC ()<UITableViewDelegate, TPTextAlertViewDelegate, SDNWANSettingVCDataSourceDelegate, TPSelectIndexDelegate, SDNQuickSetupSelectionTableViewControllerDelegate> @property (nonatomic, strong) TPKeyboardAvoidingTableView *tableView; @property (nonatomic, strong) SDNWANSettingVCDataSource *dataSource; @property (nonatomic, strong) DMSDNWanPortInfo *currentWanSetting; @property (nonatomic, strong) DMSDNWanPortInfo *originalWanSetting; @property (nonatomic, strong) TPAlertViewFactory *alertViewFactory; @property (nonatomic, strong) TPTextAlertView *discardAlertView; @property (nonatomic, assign) BOOL isSupportReadonly; @property (nonatomic, copy) NSArray<DMSDNInternetSupportProtocolInfo *> *ipv4ConnectionTypeList; @property (nonatomic, copy) NSArray<DMSDNInternetSupportProtocolInfo *> *ipv6ConnectionTypeList; @property (nonatomic, copy) NSArray<NSNumber *> *ipv4InternetVlanPriorityList; @property (nonatomic, copy) NSArray<NSNumber *> *ipv6GetAddressMethodList; @property (nonatomic, strong) DMSDNInternetWanLanPortSetting *currentPortDetail; @end @implementation SDNWANSettingVC - (instancetype)initWithWanSetting:(DMSDNWanPortInfo *)wanPortInfo andPortDetail:(DMSDNInternetWanLanPortSetting *)portDetail { self = [super init]; if (self) { self.currentWanSetting = [wanPortInfo copy]; self.originalWanSetting = [wanPortInfo copy]; if ([self supportVLANIdRequired]) { self.originalWanSetting.ipv4Info.isInternetVlanEnable = YES; self.currentWanSetting.ipv4Info.isInternetVlanEnable = YES; } self.currentPortDetail = [portDetail copy]; } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.isSupportReadonly = ![SDNV6PermissionTool shareInstance].supportCompatibleSiteNetworkSaveConfig; [self buildNav]; [self buildView]; [self reloadView]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.tpbPrivacyProtectionEnabled = YES; } //MARK: - view - (void)buildView { self.dataSource = [[SDNWANSettingVCDataSource alloc] init]; //以下两种情况可以从json文件中读取vlanid,只读 NSInteger dslInfoNum = [[TPDslIspTool shareInstance] getCurrentDslInfoWithCountry:[[TPSimpleCountryTool shareInstance] getCountryDetailWithSimpleCountry:self.currentWanSetting.dslSettingInfo.location] andIspNum:self.currentWanSetting.dslSettingInfo.isp].count; self.dataSource.dslVlanIdReadOnly = dslInfoNum == 7 || dslInfoNum == 4; self.tableView.dataSource = self.dataSource; self.dataSource.delegate = self; [self.dataSource registerClassInTableView:self.tableView]; [self.view addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.leading.trailing.equalTo(self.mas_tpSafeAreaLayoutGuide); make.bottom.equalTo(self.view); }]; } - (void)buildNav { [self.tpNavigationController setNavigationBarTitleFont:[UIFont tpr20Regular] andColor:[UIColor tpbTextPrimary]]; [self setNavigationBarTitle:self.currentWanSetting.portName]; self.navigationItem.leftBarButtonItem = [self createBarButtonItemItemWithStyle:TPViewControllerBarButtonItemStyleBackInWhite andTarget:self andAction:@selector(clickBack)]; self.navigationItem.rightBarButtonItem = [self tpbCreateRightBarButtonItemWithText:gGlobal.textSave andTextColor:[UIColor tpbTextPrimary] andTarget:self andAction:@selector(clickSave)]; } //MARK: - tableviewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated: YES]; if ([kSDNInternetWanPortIpv4ConnectionTypeCellKey isEqualToString:self.dataSource.modelList[indexPath.section].cellModelList[indexPath.row].key]) { if (!self.dataSource.modelList[indexPath.section].cellModelList[indexPath.row].isReadOnly) { [self clickIpv4ConnectionType]; } } if ([kSDNInternetWanPortIpv4VlanPriorityCellKey isEqualToString:self.dataSource.modelList[indexPath.section].cellModelList[indexPath.row].key]) { if (DMSDNInternetIpv4ConnectionTypePPPoA != self.currentWanSetting.ipv4Info.connectionType && DMSDNInternetIpv4ConnectionTypeIPoA != self.currentWanSetting.ipv4Info.connectionType) { [self clickIpv4VlanPriority]; } } if ([kSDNInternetWanPortIpv6ConnectionTypeCellKey isEqualToString:self.dataSource.modelList[indexPath.section].cellModelList[indexPath.row].key]) { [self clickIpv6ConnectionType]; } if ([kSDNInternetWanPortIpv6GetAddressMethodCellKey isEqualToString:self.dataSource.modelList[indexPath.section].cellModelList[indexPath.row].key]) { [self clickIpv6GetAddressMethod]; } if ([kSDNInternetWanPortISPSelectionCellKey isEqualToString:self.dataSource.modelList[indexPath.section].cellModelList[indexPath.row].key]) { [self didClickISP]; } if ([kSDNInternetWanPortLocationSelectionCellKey isEqualToString:self.dataSource.modelList[indexPath.section].cellModelList[indexPath.row].key]) { [self didClickRegin]; } if ([kSDNInternetWanPortModuSelectionCellKey isEqualToString:self.dataSource.modelList[indexPath.section].cellModelList[indexPath.row].key]) { if (!self.dataSource.modelList[indexPath.section].cellModelList[indexPath.row].isReadOnly) { [self didClickDSLModuType]; } } } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { if (IsEmptyString(self.dataSource.modelList[section].sectionTip)) { return [[UIView alloc] init]; } else { return [TPBCommonTableHelper createSectionFooterView:self.dataSource.modelList[section].sectionTip]; } } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if (IsEmptyString(self.dataSource.modelList[section].sectionTitle)) { return [[UIView alloc] init]; } else { return [TPBCommonTableHelper createSectionHeaderView:self.dataSource.modelList[section].sectionTitle]; } } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { if (IsEmptyString(self.dataSource.modelList[section].sectionTip)) { return TPBDesign.list.sectionFooterHeight; } else { return UITableViewAutomaticDimension; } } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if (section == 0) { return TPBDesign.list.sectionHeaderHeight; } else { if (IsEmptyString(self.dataSource.modelList[section].sectionTitle)) { return TPBDesign.list.sectionHeaderHeight; } else { return UITableViewAutomaticDimension; } } } //MARK: - 点击事件 - (void)clickSave { [self hideKeyboard]; self.tableView.editing = NO; if ([self.dataSource isCheckAllOk]) { [self.dataSource updateInputGroupViewData]; if (self.didClickSave) { self.didClickSave(self.currentWanSetting); } [self.navigationController popViewControllerAnimated:YES]; } } - (void)clickBack { [self hideKeyboard]; self.tableView.editing = NO; if (![self.originalWanSetting isEqual:self.currentWanSetting]) { [self.discardAlertView show]; } else { [self.navigationController popViewControllerAnimated:YES]; } } - (void)clickIpv4ConnectionType { NSMutableArray<NSString *> *ipv4ConnectionStringList = [[NSMutableArray alloc] init]; int index = 0; // for (int i = 0; i < self.ipv4ConnectionTypeList.count; i++) // { // [ipv4ConnectionStringList addObject:self.ipv4ConnectionTypeList[i].translateName]; // if (self.currentWanSetting.ipv4Info.connectionType == self.ipv4ConnectionTypeList[i].protocolId) // { // index = i; // } // } for (int i = 0; i < self.ipv4ConnectionTypeList.count; i++) { NSString *tempConnection = [TPSDNControllerCommonMethod transformToIpv4DslVDSLTranslateNameWithProtocolId: self.ipv4ConnectionTypeList[i].protocolId]; if (!IsEmptyString(tempConnection)){ [ipv4ConnectionStringList addObject:tempConnection]; } if (self.currentWanSetting.ipv4Info.connectionType == self.ipv4ConnectionTypeList[i].protocolId) { index = i; } } if (ModulationTypeADSL == self.currentWanSetting.dslSettingInfo.modulationType && self.currentPortDetail.type == DMSDNInternetPortTypeDsl) { for (int i = 0; i < self.ipv4ConnectionTypeList.count; i++) { NSString *tempConnection = [TPSDNControllerCommonMethod transformToIpv4PppoaAndIpoaTranslateNameWithProtocolId: self.ipv4ConnectionTypeList[i].protocolId]; if (!IsEmptyString(tempConnection)){ [ipv4ConnectionStringList addObject:tempConnection]; } if (self.currentWanSetting.ipv4Info.connectionType == self.ipv4ConnectionTypeList[i].protocolId) { index = i; } } } // TPListSelectIndexVC *vc = [[TPListSelectIndexVC alloc] initWithList:[ipv4ConnectionStringList copy] andIndex:index]; // vc.flag = ipv4ConnectionTypeFlag; // [vc updateTitle:gMeshQuickSetup.ipv4Connection]; // vc.delegate = self; // [self.tpNavigationController pushViewController:vc animated:YES]; OMDListSelectPopUpView *view = [OMDListSelectPopUpView new]; [view updateWithTitle:gMeshQuickSetup.ipv4Connection allItemStrArray:[ipv4ConnectionStringList copy] selectedStr:[ipv4ConnectionStringList copy][index]]; TPWeakSelf view.selectionDidChangeCallback = ^(NSUInteger selectedIndex, OMDSelectedItem * _Nonnull item) { TPStrongSelf if (selectedIndex < self.ipv4ConnectionTypeList.count && selectedIndex >= 0) { _self.currentWanSetting.ipv4Info.connectionType = self.ipv4ConnectionTypeList[selectedIndex].protocolId; [_self reloadView]; } }; [view show]; } - (void)clickIpv4VlanPriority { NSMutableArray<NSString *> *ipv4VlanPriorityStringList = [[NSMutableArray alloc] init]; int index = 0; for (int i = 0; i < self.ipv4InternetVlanPriorityList.count; i++) { [ipv4VlanPriorityStringList addObject:[NSString stringWithFormat:@"%@", self.ipv4InternetVlanPriorityList[i]]]; if (self.currentWanSetting.ipv4Info.vlanPriority == [self.ipv4InternetVlanPriorityList[i] integerValue]) { index = i; } } OMDListSelectPopUpView *view = [OMDListSelectPopUpView new]; [view updateWithTitle:gMeshQuickSetup.internetVlanPriority allItemStrArray:[ipv4VlanPriorityStringList copy] selectedStr:[ipv4VlanPriorityStringList copy][index]]; TPWeakSelf view.selectionDidChangeCallback = ^(NSUInteger selectedIndex, OMDSelectedItem * _Nonnull item) { TPStrongSelf if (selectedIndex >= 0 && selectedIndex < self.ipv4InternetVlanPriorityList.count) { _self.currentWanSetting.ipv4Info.vlanPriority = [_self.ipv4InternetVlanPriorityList[selectedIndex] integerValue]; [_self reloadView]; } }; [view show]; } - (void)clickIpv6ConnectionType { NSMutableArray<NSString *> *ipv6ConnectionStringList = [[NSMutableArray alloc] init]; int index = 0; for (int i = 0; i < self.ipv6ConnectionTypeList.count; i++) { [ipv6ConnectionStringList addObject:self.ipv6ConnectionTypeList[i].translateName]; if (self.currentWanSetting.ipv6Info.connectionType == self.ipv6ConnectionTypeList[i].protocolId) { index = i; } } // TPListSelectIndexVC *vc = [[TPListSelectIndexVC alloc] initWithList:[ipv6ConnectionStringList copy] andIndex:index]; // vc.flag = ipv6ConnectionTypeFlag; // [vc updateTitle:gMeshQuickSetup.ipv6Connection]; // vc.delegate = self; // [self.tpNavigationController pushViewController:vc animated:YES]; OMDListSelectPopUpView *view = [OMDListSelectPopUpView new]; [view updateWithTitle:gMeshQuickSetup.ipv6Connection allItemStrArray:[ipv6ConnectionStringList copy] selectedStr:[ipv6ConnectionStringList copy][index]]; TPWeakSelf view.selectionDidChangeCallback = ^(NSUInteger selectedIndex, OMDSelectedItem * _Nonnull item) { TPStrongSelf if (selectedIndex < self.ipv6ConnectionTypeList.count && selectedIndex >= 0) { _self.currentWanSetting.ipv6Info.connectionType = self.ipv6ConnectionTypeList[selectedIndex].protocolId; [_self reloadView]; } }; [view show]; } - (void)clickIpv6GetAddressMethod { [self updateIpv6GetMethodList]; NSMutableArray<NSString *> *ipv6GetAddressMethodStringList = [[NSMutableArray alloc] init]; int index = 0; for (int i = 0; i < self.ipv6GetAddressMethodList.count; i++) { [ipv6GetAddressMethodStringList addObject:[TPSDNControllerCommonMethod getIpv6AddressMethodWithMethod:[TPSDNControllerCommonMethods transformToDMSDNInternetGetIpv6AddressMethodWithMethod:[self.ipv6GetAddressMethodList[i] integerValue]]]]; if (DMSDNInternetIpv6ConnectionTypePppoe == self.currentWanSetting.ipv6Info.connectionType) { if (self.currentWanSetting.ipv6Info.pppoeInfo.getIpv6AddressMethod == [TPSDNControllerCommonMethods transformToDMSDNInternetGetIpv6AddressMethodWithMethod:[self.ipv6GetAddressMethodList[i] integerValue]]) { index = i; } } else if (DMSDNInternetIpv6ConnectionTypeDynamic == self.currentWanSetting.ipv6Info.connectionType) { if (self.currentWanSetting.ipv6Info.dynamicInfo.getIpv6AddressMethod == [TPSDNControllerCommonMethods transformToDMSDNInternetGetIpv6AddressMethodWithMethod:[self.ipv6GetAddressMethodList[i] integerValue]]) { index = i; } } } // TPListSelectIndexVC *vc = [[TPListSelectIndexVC alloc] initWithList:[ipv6GetAddressMethodStringList copy] andIndex:index]; // vc.flag = ipv6GetAddressMethodFlag; // [vc updateTitle:gMeshQuickSetup.getIpv6Address]; // vc.delegate = self; // [self.tpNavigationController pushViewController:vc animated:YES]; OMDListSelectPopUpView *view = [OMDListSelectPopUpView new]; [view updateWithTitle:gMeshQuickSetup.getIpv6Address allItemStrArray:[ipv6GetAddressMethodStringList copy] selectedStr:[ipv6GetAddressMethodStringList copy][index]]; TPWeakSelf view.selectionDidChangeCallback = ^(NSUInteger selectedIndex, OMDSelectedItem * _Nonnull item) { TPStrongSelf if (selectedIndex < _self.ipv6GetAddressMethodList.count) { if (DMSDNInternetIpv6ConnectionTypePppoe == _self.currentWanSetting.ipv6Info.connectionType) { _self.currentWanSetting.ipv6Info.pppoeInfo.getIpv6AddressMethod = [TPSDNControllerCommonMethods transformToDMSDNInternetGetIpv6AddressMethodWithMethod:[_self.ipv6GetAddressMethodList[selectedIndex] integerValue]]; } else if (DMSDNInternetIpv6ConnectionTypeDynamic == self.currentWanSetting.ipv6Info.connectionType) { _self.currentWanSetting.ipv6Info.dynamicInfo.getIpv6AddressMethod = [TPSDNControllerCommonMethods transformToDMSDNInternetGetIpv6AddressMethodWithMethod:[_self.ipv6GetAddressMethodList[selectedIndex] integerValue]]; } } [_self reloadView]; }; [view show]; } - (void)didClickISP { NSString *countryDetail = [[TPSimpleCountryTool shareInstance] getCountryDetailWithSimpleCountry:self.currentWanSetting.dslSettingInfo.location]; NSArray *ispArr = [[TPDslIspTool shareInstance] getIspListWithCountry:countryDetail]; NSString *selectIsp = [[TPDslIspTool shareInstance] getCurrentIspWithCountry:countryDetail andIspNum:self.currentWanSetting.dslSettingInfo.isp]; TPBGeneralSelectionController *vc = [[TPBGeneralSelectionController alloc] initWithTitle:gMeshQuickSetup.sLGatewayIsp isPresented:YES confirmButtonTitle:gSDNGlobal.done allItemArray:[self filterIspArr:ispArr] selectedItem:selectIsp supportSearch:NO supportIndex:NO autoSort:NO checkInFront:NO]; TPWeakSelf vc.selectionDidChangeCallback = ^(NSUInteger selectedIndex, NSString * _Nonnull selectedItem) { TPStrongSelf DDLogDebug(@"选择运营商完毕,选择了名称为:[%@]", selectedItem); if (!IsEmptyString(selectedItem)) { NSString *countryDetail = [[TPSimpleCountryTool shareInstance] getCountryDetailWithSimpleCountry:_self.currentWanSetting.dslSettingInfo.location]; _self.currentWanSetting.dslSettingInfo.isp = [[TPDslIspTool shareInstance] getIspNumWithCountry:countryDetail andIsp:selectedItem]; NSArray *dslInfoArray = [[TPDslIspTool shareInstance] getCurrentDslInfoWithCountry:countryDetail andIspNum:_self.currentWanSetting.dslSettingInfo.isp]; if (dslInfoArray.count == 7) { _self.currentWanSetting.dslSettingInfo.modulationType = ModulationTypeADSL; if ([dslInfoArray[1] respondsToSelector:@selector(integerValue)]) { _self.currentWanSetting.dslSettingInfo.vpi = [dslInfoArray[1] integerValue]; } if ([dslInfoArray[2] respondsToSelector:@selector(integerValue)]) { _self.currentWanSetting.dslSettingInfo.vci = [dslInfoArray[2] integerValue]; } _self.currentWanSetting.ipv4Info.connectionType = [SLDSLDataModelTransformMethods getIpv4ConnectionWithStr:dslInfoArray[3]]; if (!IsEmptyString(dslInfoArray[4])) { _self.currentWanSetting.dslSettingInfo.encapMode = [SLDSLDataModelTransformMethods getEncapModeWithStr:dslInfoArray[4]]; } if ([dslInfoArray[5] respondsToSelector:@selector(integerValue)]) { _self.currentWanSetting.ipv4Info.vlanId = [dslInfoArray[5] integerValue]; _self.dataSource.dslVlanIdReadOnly = YES; } } else if (dslInfoArray.count == 6) { _self.currentWanSetting.dslSettingInfo.modulationType = ModulationTypeADSL; if ([dslInfoArray[1] respondsToSelector:@selector(integerValue)]) { _self.currentWanSetting.dslSettingInfo.vpi = [dslInfoArray[1] integerValue]; } if ([dslInfoArray[2] respondsToSelector:@selector(integerValue)]) { _self.currentWanSetting.dslSettingInfo.vci = [dslInfoArray[2] integerValue]; } _self.currentWanSetting.ipv4Info.connectionType = [SLDSLDataModelTransformMethods getIpv4ConnectionWithStr:dslInfoArray[3]]; if (!IsEmptyString(dslInfoArray[4])) { _self.currentWanSetting.dslSettingInfo.encapMode = [SLDSLDataModelTransformMethods getEncapModeWithStr:dslInfoArray[4]]; } _self.dataSource.dslVlanIdReadOnly = NO; } else if (dslInfoArray.count == 4) { if ([dslInfoArray[1] respondsToSelector:@selector(integerValue)]) { _self.currentWanSetting.ipv4Info.vlanId = [dslInfoArray[1] integerValue]; _self.dataSource.dslVlanIdReadOnly = YES; } _self.currentWanSetting.dslSettingInfo.modulationType = ModulationTypeVDSL; _self.currentWanSetting.ipv4Info.connectionType = [SLDSLDataModelTransformMethods getIpv4ConnectionWithStr:dslInfoArray[2]]; } else { _self.dataSource.dslVlanIdReadOnly = NO; } _self.currentWanSetting.dslSettingInfo.isp = [[TPDslIspTool shareInstance] getIspNumWithCountry:countryDetail andIsp:selectedItem]; [_self reloadView]; } }; UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:vc]; [self.tpNavigationController presentViewController:navVC animated:YES completion:nil]; } - (void)didClickRegin { NSArray *reginArray = [TPDslIspTool getReginInternationalList]; NSString *countryDetail = [[TPSimpleCountryTool shareInstance] getCountryDetailWithSimpleCountry:self.currentWanSetting.dslSettingInfo.location]; SDNQSSelectCountryTableViewController *vc = [[SDNQSSelectCountryTableViewController alloc]initWithTitle:gMeshQuickSetup.location totalItems:reginArray selectedItem:countryDetail]; vc.delegate = self; UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:vc]; [self.tpNavigationController presentViewController:navVC animated:YES completion:nil]; } - (void)didClickDSLModuType { // SLSelectDSLMudoViewController *vc = [[SLSelectDSLMudoViewController alloc] init]; // vc.selectType = self.currentWanSetting.dslSettingInfo.modulationType; // TPWeakSelf // vc.handleSourceBlock = ^(ModulationType selectType) { // TPStrongSelf // if (_self.currentWanSetting.dslSettingInfo.modulationType != selectType) // { // _self.currentWanSetting.ipv4Info.connectionType = DMSDNInternetIpv4ConnectionTypeDhcp; // } // _self.currentWanSetting.dslSettingInfo.modulationType = selectType; // [_self reloadView]; // }; // [self.tpNavigationController pushViewController:vc animated:YES]; NSArray *allStrArray = @[gMeshQuickSetup.dSLModulationTypeVDSL, gMeshQuickSetup.dSLModulationTypeADSL]; NSString *selectedStr = self.currentWanSetting.dslSettingInfo.modulationType == ModulationTypeVDSL? gMeshQuickSetup.dSLModulationTypeVDSL : gMeshQuickSetup.dSLModulationTypeADSL; OMDListSelectPopUpView *view = [OMDListSelectPopUpView new]; [view updateWithTitle:gMeshQuickSetup.dSLModulationType allItemStrArray:allStrArray selectedStr:selectedStr]; TPWeakSelf view.selectionDidChangeCallback = ^(NSUInteger selectedIndex, OMDSelectedItem * _Nonnull item) { TPStrongSelf ModulationType selectType = [item.title isEqualToString:gMeshQuickSetup.dSLModulationTypeVDSL]? ModulationTypeVDSL : ModulationTypeADSL; if (_self.currentWanSetting.dslSettingInfo.modulationType != selectType) { _self.currentWanSetting.ipv4Info.connectionType = DMSDNInternetIpv4ConnectionTypeDhcp; } _self.currentWanSetting.dslSettingInfo.modulationType = selectType; [_self reloadView]; }; [view show]; } //MARK: - Lazy - (UITableView *)tableView { if (_tableView == nil) { _tableView = [TPBCommonTableHelper createKeyboardAvoidingTableViewWithDelegate:self andDataSource:self.dataSource]; } return _tableView; } - (TPAlertViewFactory *)alertViewFactory { if (_alertViewFactory == nil) { _alertViewFactory = [[TPAlertViewFactory alloc] init]; } return _alertViewFactory; } - (TPTextAlertView *)discardAlertView { if (_discardAlertView == nil) { _discardAlertView = [self.alertViewFactory discardAlertViewForSDN]; _discardAlertView.alertViewDelegate = self; } return _discardAlertView; } - (NSArray<DMSDNInternetSupportProtocolInfo *> *)ipv4ConnectionTypeList { if (_ipv4ConnectionTypeList == nil) { _ipv4ConnectionTypeList = [[ALSDNSiteInternetSetting currentInstance].supportInfo.ipv4ProtocalInfoList copy]; } return _ipv4ConnectionTypeList; } - (NSArray<DMSDNInternetSupportProtocolInfo *> *)ipv6ConnectionTypeList { if (_ipv6ConnectionTypeList == nil) { _ipv6ConnectionTypeList = [[ALSDNSiteInternetSetting currentInstance].supportInfo.ipv6ProtocalInfoList copy]; } return _ipv6ConnectionTypeList; } - (NSArray<NSNumber *> *)ipv6GetAddressMethodList { if (_ipv6GetAddressMethodList == nil) { _ipv6GetAddressMethodList = [[NSArray alloc] init]; } return _ipv6GetAddressMethodList; } - (NSArray<NSNumber *> *)ipv4InternetVlanPriorityList { if (_ipv4InternetVlanPriorityList == nil) { NSMutableArray<NSNumber *> *ipv4VlanPriorityList = [[NSMutableArray alloc] init]; for (int i = 0; i < 8; i++) { [ipv4VlanPriorityList addObject:@(i)]; } _ipv4InternetVlanPriorityList = [ipv4VlanPriorityList copy]; } return _ipv4InternetVlanPriorityList; } //MARK: - alertViewDelegate - (void)alertView:(TPAlertView *)alertView clickedButtonAtIndex:(NSInteger)index { if (index == 1) { self.currentWanSetting = self.originalWanSetting; [self.navigationController popViewControllerAnimated:YES]; } } //MARK: - 页面刷新 - (void)reloadView { [self hideKeyboard]; self.tableView.editing = NO; [self.dataSource updateModelWithWanSetting:self.currentWanSetting andPortDetail:self.currentPortDetail]; [self.tableView reloadData]; if (![self.originalWanSetting isEqual:self.currentWanSetting]) { [self.navigationItem.rightBarButtonItem setEnabled:YES]; } else { [self.navigationItem.rightBarButtonItem setEnabled:NO]; } } - (void)isNeedRefreshSaveButton { if (![self.originalWanSetting isEqual:self.currentWanSetting]) { [self.navigationItem.rightBarButtonItem setEnabled:YES]; } else { [self.navigationItem.rightBarButtonItem setEnabled:NO]; } } //MARK: - SDNWANSettingVCDataSourceDelegate - (void)isNeedRefreshView { [self reloadView]; } //MARK: - SDNSelectIndexDelegate - (void)didSelectIndex:(NSInteger)index andFlag:(NSString *)flag { [self.dataSource updateInputGroupViewData]; if ([ipv4ConnectionTypeFlag isEqualToString:flag]) { if (index < self.ipv4ConnectionTypeList.count) { self.currentWanSetting.ipv4Info.connectionType = self.ipv4ConnectionTypeList[index].protocolId; } } else if ([ipv4InternetVlanPriorityFlag isEqualToString:flag]) { if (index < self.ipv4InternetVlanPriorityList.count) { self.currentWanSetting.ipv4Info.vlanPriority = [self.ipv4InternetVlanPriorityList[index] integerValue]; } } else if ([ipv6ConnectionTypeFlag isEqualToString:flag]) { if (index < self.ipv6ConnectionTypeList.count) { self.currentWanSetting.ipv6Info.connectionType = self.ipv6ConnectionTypeList[index].protocolId; } } else if ([ipv6GetAddressMethodFlag isEqualToString:flag]) { if (index < self.ipv6GetAddressMethodList.count) { if (DMSDNInternetIpv6ConnectionTypePppoe == self.currentWanSetting.ipv6Info.connectionType) { self.currentWanSetting.ipv6Info.pppoeInfo.getIpv6AddressMethod = [TPSDNControllerCommonMethods transformToDMSDNInternetGetIpv6AddressMethodWithMethod:[self.ipv6GetAddressMethodList[index] integerValue]]; } else if (DMSDNInternetIpv6ConnectionTypeDynamic == self.currentWanSetting.ipv6Info.connectionType) { self.currentWanSetting.ipv6Info.dynamicInfo.getIpv6AddressMethod = [TPSDNControllerCommonMethods transformToDMSDNInternetGetIpv6AddressMethodWithMethod:[self.ipv6GetAddressMethodList[index] integerValue]]; } } } [self reloadView]; } //MARK: UIGestureRecognizerDelegate - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { if(TPISRTL!=TPISSYSTEMRTL){ return NO; } [self clickBack]; return NO; } //MARK: Location iSP点击事件代理 - (void)didCompleteViewControllerWithTag:(NSInteger)vcTag andSelecteItem:(NSString*)selectedItem { DDLogDebug(@"选择国家地区完毕,选择了名称为:[%@]", selectedItem); if (!IsEmptyString(selectedItem)) { NSString *location = [[TPSimpleCountryTool shareInstance] getSimpleCountryWithCountryDetail:selectedItem]; if (![self.currentWanSetting.dslSettingInfo.location isEqualToString:location]) { self.currentWanSetting.dslSettingInfo.location = location; self.currentWanSetting.dslSettingInfo.isp = 0; self.dataSource.dslVlanIdReadOnly = NO; [self reloadView]; } } } //MARK: - tool - (void)updateIpv6GetMethodList { NSMutableArray<NSNumber *> *ipv6GetAddressMethodIdList = [[NSMutableArray alloc] init]; [ipv6GetAddressMethodIdList addObject:@(DMSDNInternetGetIpv6AddressMethodAuto)]; [ipv6GetAddressMethodIdList addObject:@(DMSDNInternetGetIpv6AddressMethodSlaac)]; [ipv6GetAddressMethodIdList addObject:@(DMSDNInternetGetIpv6AddressMethodDhcpv6)]; if (self.isSupportIpv6NonAddress) { [ipv6GetAddressMethodIdList addObject:@(DMSDNInternetGetIpv6AddressMethodNon_address)]; } if (DMSDNInternetIpv6ConnectionTypePppoe == self.currentWanSetting.ipv6Info.connectionType) { [ipv6GetAddressMethodIdList addObject:@(DMSDNInternetGetIpv6AddressMethodSpecified)]; } self.ipv6GetAddressMethodList = [ipv6GetAddressMethodIdList copy]; } - (void)dealloc { DDLogDebug(@"SDNWANSettingVC dealloc"); } - (BOOL)supportVLANIdRequired { return [[TPSDNControllerClient currentInstance].currentSite.componentNegotiate supportForSiteComponent:DMSDNComponentIdInternetVlanIdRequired ForVersionCode:1]; } - (NSArray *)filterIspArr:(NSArray *)array { NSArray *sortedArray = [array sortedArrayUsingSelector:@selector(compare:)]; NSMutableArray *ispArr = sortedArray.mutableCopy; if ([ispArr containsObject:gMeshQuickSetup.dslISPOther]) { [ispArr removeObject:gMeshQuickSetup.dslISPOther]; } [ispArr addObject:gMeshQuickSetup.dslISPOther]; return ispArr.copy; } @end
最新发布
12-11
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值