许多视图控制器的单个 uiview

single uiview for many viewcontrollers

我有一个 viewcontroller 将其命名为 viewcontroller1。我已将一个 uiview 拖到此 viewcontroller 并向该视图添加了一些带有操作的按钮。并且还为该 uiview 创建了一个自定义视图 class。此 uiview 用作 viewcontroller1 的顶部栏。我还有一些 viewcontrollers,我也通过使用 viewwithtag 方法获取 uiview 来为那些 viewcontrollers 使用相同的 uiview。但是当我使用这种方法时,代表们不工作。当从 uiview class 调用委托时,我获得了 thread1 exc_bad 访问权限。任何帮助表示赞赏。 这是我的 uiview class

//topview.h

@protocol buttonprotocol <NSObject>

-(void) button1pressed;
-(void) button2pressed;

@end
#import <UIKit/UIKit.h>

@interface topview : UIView
- (IBAction)button1action:(id)sender;
- (IBAction)button2action:(id)sender;
@property (nonatomic,assign) id <buttonprotocol> delegate;
@property (weak, nonatomic) IBOutlet UIButton *button1;
@property (weak, nonatomic) IBOutlet UIButton *button2;
@end

//topview.m

#import "topview.h"

@implementation topview

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
- (IBAction)button1action:(id)sender
{
    [self.delegate button1pressed];
}
- (IBAction)button2action:(id)sender
{
    [self.delegate button2pressed];
}
@end

下面给出的是第一个viewcontroller

// ViewController.h

#import <UIKit/UIKit.h>
#import "topview.h"
#import "button1ViewController.h"
#import "button2ViewController.h"
@interface ViewController : UIViewController
<buttonprotocol>
@property (weak, nonatomic) IBOutlet topview *topviewobj;


@end

// ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    _topviewobj.delegate=self;
    UIView *newview=[[UIView alloc]initWithFrame:CGRectMake(0, 250, 320, 100)];
    newview=[self.view viewWithTag:1];
    [self.view addSubview:newview];
    // Do any additional setup after loading the view, typically from a nib.
}
-(void) button1pressed
{
    UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    button1ViewController *vc=[storyboard instantiateViewControllerWithIdentifier:@"button1ViewController"];
    [self presentViewController:vc animated:NO completion:nil];
}
-(void) button2pressed
{
    UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    button2ViewController *vc=[storyboard instantiateViewControllerWithIdentifier:@"button2ViewController"];
    [self presentViewController:vc animated:NO completion:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

下面给出的是点击第一个按钮时 viewcontroller 的视图控制器 class。

// 按钮 1ViewController.h

#import <UIKit/UIKit.h>
#import "topview.h"
#import "ViewController.h"
#import "button2ViewController.h"
@class topview;
@interface button1ViewController : UIViewController
<buttonprotocol>
{
    UIView *newview;
    topview *topviewobj;
}
@end

// 按钮 1ViewController.m

#import "button1ViewController.h"

@interface button1ViewController ()

@end

@implementation button1ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    topviewobj=[topview new];
    topviewobj.delegate=self;
    NSLog(@"%@",topviewobj.delegate);
    self.view.backgroundColor=[UIColor purpleColor];
    UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ViewController *vc=[storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
    newview=[[UIView alloc]init];
    newview=[vc.view viewWithTag:1];
    UIButton *backbutton=[[UIButton alloc]init];
    [backbutton setTitle:@"Back" forState:UIControlStateNormal];
    [backbutton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [backbutton setFrame:CGRectMake(5, 30, 60, 30)];
    [backbutton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [newview addSubview:backbutton];
    [self.view addSubview:newview];
    // Do any additional setup after loading the view.
}
-(void) back
{
    UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ViewController *vc=[storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
    [self presentViewController:vc animated:NO completion:nil];
}
-(void) button1pressed
{
    UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    button1ViewController *vc=[storyboard instantiateViewControllerWithIdentifier:@"button1ViewController"];
    [self presentViewController:vc animated:NO completion:nil];
}
-(void) button2pressed
{
    UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    button2ViewController *vc=[storyboard instantiateViewControllerWithIdentifier:@"button2ViewController"];
    [self presentViewController:vc animated:NO completion:nil];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

下面给出的是 class for viewcontroller for viewcontroller 当点击第二个按钮时。

// 按钮 2ViewController.h

#import <UIKit/UIKit.h>
#import "topview.h"
#import "ViewController.h"
@class topview;
@interface button2ViewController : UIViewController
<buttonprotocol>
{
    UIView *newview;
    topview *topviewobj;
}
@end

// 按钮 2ViewController.m

#import "button2ViewController.h"

@interface button2ViewController ()

@end

@implementation button2ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    topviewobj=[topview new];
    topviewobj.delegate=self;
    NSLog(@"%@",topviewobj.delegate);
    self.view.backgroundColor=[UIColor purpleColor];
    UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ViewController *vc=[storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
    newview=[[UIView alloc]init];
    newview=[vc.view viewWithTag:1];
    UIButton *backbutton=[[UIButton alloc]init];
    [backbutton setTitle:@"Back" forState:UIControlStateNormal];
    [backbutton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [backbutton setFrame:CGRectMake(5, 30, 60, 30)];
    [backbutton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [newview addSubview:backbutton];
    [self.view addSubview:newview];
    // Do any additional setup after loading the view.
}
-(void) back
{
    UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ViewController *vc=[storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
    [self presentViewController:vc animated:NO completion:nil];
}
-(void) button1pressed
{
    UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    button1ViewController *vc=[storyboard instantiateViewControllerWithIdentifier:@"button1ViewController"];
    [self presentViewController:vc animated:NO completion:nil];
}
-(void) button2pressed
{
//    UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
//    button2ViewController *vc=[storyboard instantiateViewControllerWithIdentifier:@"button2ViewController"];
//    [self presentViewController:vc animated:NO completion:nil];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

这就是我的全部。由于隐私问题,我无法显示我的原始代码。这是为显示问题而创建的示例。由于我的声誉低,无法上传图片。

这看起来很可疑...

UIView *newview=[[UIView alloc]initWithFrame:CGRectMake(0, 250, 320, 100)];
newview=[self.view viewWithTag:1];
[self.view addSubview:newview];

如果下一行要使用 viewWithTag,则不必分配初始化新视图,因为该视图已经初始化。同样在下一行中,您在刚刚获得视图的同一件事上调用 addSubView,只有在视图已添加的情况下才有效。

编辑

进一步检查您的代码后,我明白了您要做什么。有几件事需要注意。如果你想将对象传递给新的视图控制器,你可以在创建新的视图控制器时这样做。

UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
button1ViewController *vc=[storyboard instantiateViewControllerWithIdentifier:@"button1ViewController"];

//if button1ViewController had a property for topviewobj instead of an instance variable.
button1ViewController.topviewobj = (topview *)[self.view viewWithTag:1];
[self presentViewController:vc animated:NO completion:nil];

//in button1ViewController viewDidLoad
self.topviewobj.delegate = self;
[self.view addSubView:self.topviewobj];

话虽如此,我认为您真正想做的是研究笔尖或仅在代码中创建顶视图。

如果只是在代码中,它会像

_topviewobj = [[topview alloc]initWithFrame:CGRectMake(0,250, 320, 100)];
_topviewobj.delegate = self;
[self.view addSubview:_topviewobj];

这也可能会在未来引起问题...

-(void) back
{
    UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ViewController *vc=[storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
    [self presentViewController:vc animated:NO completion:nil];
}

一遍又一遍地创建新的 UIViewController 并不理想。试试这个...

-(void) back
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

正如我之前所说,我会研究 nib 或只是创建一个 UIView 子类和 alloc/init,设置委托,并在需要时添加为子视图。

我希望所有这些都有意义并有所帮助。