如何通过点击另一个UIIimage
内的UIButton
来展示UIViewController
?我想添加相同的UIButton
命令来向SecondViewController添加图像。原谅我的可怜问题。
myProtocol.h
#import <Foundation/Foundation.h>
@protocol myProtocol <NSObject>
-(UIImage *)transferImage;
@end
ViewController.h
#import "SecondClass.h"
@interface ViewController : UIViewController<myProtocol, UINavigationControllerDelegate>{
UIView *view;
}
@property (nonatomic,retain) UIImageView *imageView;
- (IBAction)sendImage:(id)sender;
@end
ViewController.m
#import "ViewController.h"
#import "SecondViewController.h"
#import "myProtocol.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad{
[super viewDidLoad];
_imageView = [[UIImageView alloc]initWithImage:
[UIImage imageNamed:@"VoodooVibe@2x.png"]];
[view addSubview:_imageView];
NSLog(@"I am in VC.m");
}
-(UIImage *)transferImage{
NSLog(@"I am in transferImage");
return _imageView.image;
}
- (IBAction)sendImage:(id)sender {
SecondViewController *secClass = [[SecondViewController alloc]init];
secClass.delegate=self;
[secClass callTransfer];
NSLog(@"I am in sender");
[self.navigationController pushViewController:secClass animated:YES];
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
SecondViewController.h
#import <UIKit/UIKit.h>
#import "myProtocol.h"
#import "ViewController.h"
@interface SecondViewController :UIViewController
<myProtocol,UINavigationControllerDelegate> {
UIView *secondView;
IBOutlet UIImageView *myImage;
id <myProtocol> myDelegate;
}
@property (nonatomic,retain) UIImageView *myImage;
@property(nonatomic,assign) id delegate;
-(void)callTransfer;
@end
SecondViewController.m
#import "SecondViewController.h"
#import "ViewController.h"
#import "myProtocol.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
@synthesize delegate,myImage;
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view.
[secondView addSubview:myImage];
}
-(void)callTransfer{
myImage.image=[delegate performSelector:@selector(transferImage)];
myImage.image=[UIImage imageNamed:@"VoodooVibe@2x.png"];
NSLog(@"%@",myImage.image);
NSLog(@"I am in call transfer");
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
答案 0 :(得分:0)
通常应该使用代理,如果您在UIViewController
中包含一个类,它将执行某些操作并在特定过程完成时通过委托方法通知您。但在这种情况下,您可以设置UIImage
两次。在您的delegate
之后,第二次以编程方式设置UIImage
。
你不应该做任何事情,比如从外面调用初始化第二个UIImage
UIViewController
的方法。只需调用viewDidLoad
内的所有内容,您就不必关心它,因为UIViewController
会自行处理它。
您只需在UIImageView
内插入SecondViewController
并将其连接到您的头文件即可。然后你可以在m里面访问它。文件。我遇到的问题是我第一次使用jpg
而不是png
,但更改后缀后一切正常。
<强> ViewController.m 强>
- (IBAction)sendImage:(id)sender {
SecondViewController *secClass = [[SecondViewController alloc] init];
[secClass setImageName:@"pic.png"];
[self.navigationController pushViewController:secClass
animated:YES];
}
<强> SecondViewController.h 强>
@interface SecondViewController : UIViewController
@property (strong, nonatomic)NSString *imageName;
@property (weak, nonatomic) IBOutlet UIImageView *myImage;
@end
SecondViewController.m (只有这两行)
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[_myImage setImage:[UIImage imageNamed:_imageName]];
}