iOS 堆栈,windowView个人见解

先创建一个堆栈类

#import <Foundation/Foundation.h>

@interface XMStack : NSObject
///推入堆
- (void)push:(id) obj;
///推出堆
- (id)pop;
///返回顶,不删除堆内元素
- (id)top;
///堆内是否还有元素
- (BOOL) isEmpty;
///堆内元素数量
- (NSInteger) count;

@end


#import "XMStack.h"

@interface XMStack()

@property NSMutableArray *array;

@property NSInteger currentIndex;

@end

@implementation XMStack

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.array = [[NSMutableArray alloc] initWithCapacity:32];
    }
    return self;
}

- (void)push:(id) obj{
    if (self.currentIndex >= self.array.count-1) {
        NSMutableArray *newArray = [[NSMutableArray alloc] initWithCapacity:self.array.count*2];
        for (id obj in self.array) {
            [newArray addObject:obj];
        }
        self.array = newArray;
    }
    [self.array addObject:obj];
    self.currentIndex++;
}

- (id)pop{
    if ([self isEmpty]) {
        id obj = [self.array objectAtIndex:self.currentIndex - 1];
        [self.array removeObjectAtIndex:self.currentIndex - 1];
        self.currentIndex--;
        return obj;
    }else{
        return @"Empty";
    }
    
}
- (id)top{
    if ([self isEmpty]) {
        return self.array[self.currentIndex];
    }else{
        return @"Empty";
    }
}
- (BOOL) isEmpty{
    return self.currentIndex>0;
}
- (NSInteger) count{
    return self.currentIndex;
}
@end



windowView模型类


#import <UIKit/UIKit.h>
#import "XMStack.h"
NS_ASSUME_NONNULL_BEGIN

@interface WindowViewModel : UIView

@property (nonatomic,strong) XMStack *stack;

@property (nonatomic,strong) UIButton *leftBtn;

@property (nonatomic,strong) UIButton *rightBtn;

@end


#import "WindowViewModel.h"

@implementation WindowViewModel

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.stack = [[XMStack alloc] init];
        
        self.leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        
        [self.leftBtn setBackgroundColor:[UIColor blueColor]];
        
        UILabel *btnLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        [btnLabel setText:@"back"];
        [btnLabel setTextAlignment:NSTextAlignmentCenter];
        [btnLabel setTextColor:[UIColor whiteColor]];
          
        self.rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width-50, 0, 50, 50)];
        
        [self.rightBtn setBackgroundColor:[UIColor blueColor]];
        
        UILabel *btnLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        [btnLabel2 setText:@"next"];
        [btnLabel2 setTextAlignment:NSTextAlignmentCenter];
        [btnLabel2 setTextColor:[UIColor whiteColor]];
        
        [self.leftBtn addSubview:btnLabel];
        [self.rightBtn addSubview:btnLabel2];
        
        [self addSubview:self.leftBtn];
        [self addSubview:self.rightBtn];
        [self setBackgroundColor:[UIColor redColor]];
    }
    return self;
}

@end




模拟演示

导入#import “XMStack.h”

导入#import “WindowViewModel.h”

1.实例化一个模型windowView

	WindowViewModel *view = [[WindowViewModel alloc] initWithFrame:CGRectMake(100, 100, 200, 300)];
    
    [self.view addSubview:view];


2.实例化一个模型vc 添加进windowView,并push进windowView的堆栈中

//模拟vc
	UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 200, 250)];
    [subView setBackgroundColor:[UIColor whiteColor]];
    
    UILabel *subLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 250)];
    [subLabel setText:@"1"];
    [subLabel setTextAlignment:NSTextAlignmentCenter];
    
    
    [subView addSubview:subLabel];
    //添加进windowView
	[view addSubview:subView];
	//放入堆栈
    [view.stack push:subView];


3.给windowView的左右按钮绑定方法

	[view.leftBtn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    
    [view.rightBtn addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside];



4.实现跳转页面方法,和返回上一页方法

//返回上个页面
-(void) back:(UIButton *)btn{
    
    NSLog(@"back");
    
    WindowViewModel *view = (WindowViewModel *)btn.superview;
  
    UIView *subView = [view.stack pop];
    
    [subView removeFromSuperview];
    
 
    
}

//跳转下个页面
-(void) next:(UIButton *)btn{
    NSLog(@"next");
    
    WindowViewModel *view = (WindowViewModel *)btn.superview;
    
    UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 200, 250)];
    [subView setBackgroundColor:[UIColor whiteColor]];
    
    UILabel *subLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 250)];
    [subLabel setText:[NSString stringWithFormat:@"%ld",view.stack.count+1]];
    [subLabel setTextAlignment:NSTextAlignmentCenter];
    
    [subView addSubview:subLabel];
    
    [view addSubview:subView];
    [view.stack push:subView];
    
}



如果windowView中已经没有页面,还继续上一页会报错,很正常,防止报错在返回上个页面出加判断


	if([view.stack isEmpty]){

	}

发布了14 篇原创文章 · 获赞 7 · 访问量 5285

猜你喜欢

转载自blog.csdn.net/qq_41586150/article/details/104076362