我有一个viewcontroller将它命名为viewcontroller1。我已将uiview拖到此viewcontroller,并通过操作向该视图添加了一些按钮。此外,还为该uiview创建了自定义视图类。这个uiview充当了viewcontroller1的顶级栏。我有更多的viewcontrollers,我也通过使用viewwithtag方法获取uiview来为那些viewcontrollers使用相同的uiview。但是当我使用这种方法时,代表们没有工作。当从uiview类调用委托时,我获得了thread1 exc_bad访问权限。任何帮助表示赞赏。 这是我的uiview课程
// 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的视图控制器类。
// button1ViewController.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
// button1ViewController.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
下面给出了轻触第二个按钮时viewcontroller的viewcontroller类。
// button2ViewController.h
#import <UIKit/UIKit.h>
#import "topview.h"
#import "ViewController.h"
@class topview;
@interface button2ViewController : UIViewController
<buttonprotocol>
{
UIView *newview;
topview *topviewobj;
}
@end
// button2ViewController.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
这就是我得到的全部。由于隐私问题,我无法显示原始代码。这是为显示问题而创建的示例。由于声誉不佳,我无法上传图片。
答案 0 :(得分:1)
这看起来非常可疑......
UIView *newview=[[UIView alloc]initWithFrame:CGRectMake(0, 250, 320, 100)];
newview=[self.view viewWithTag:1];
[self.view addSubview:newview];
如果下一行要使用viewWithTag,则不应该为init分配一个新视图,因为该视图已经被引入。同样在下一行中,您可以在刚刚获得视图的同一个内容上调用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];
说实话,我认为你真正想做的就是在代码中查看笔尖或创建topview。
如果只是在代码中它就像
_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];
}
一遍又一遍地创建新的UIViewControllers并不理想。试试这个......
-(void) back
{
[self dismissViewControllerAnimated:YES completion:nil];
}
正如我之前所说,我会查看nib或只是创建一个UIView子类和alloc / init,设置委托,并在需要时添加为子视图。
我希望所有这些都有意义并有所帮助。