先创建一个堆栈类
#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
- (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的堆栈中
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];
[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]){
}