2013-10-12 67 views
-1

如何通过单击另一个UIViewController内的UIButton来显示UIIimage?我想添加到相同的UIButton将图像添加到SecondViewController的命令。原谅我这个可怜的问题。在SecondViewController中单击按钮时在ViewController中显示图像

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:@"[email protected]"]]; 
    [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:@"[email protected]"]; 
    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

你不应该做任何事情,比如调用一个方法从外部初始化第二个UIViewControllerUIImage。只需拨打viewDidLoad中的所有内容即可,您不必关心它,因为UIViewController本身就可以处理它。

您只需在您的SecondViewController中插入一个UIImageView并将其连接到您的头文件。那么你可以在里面访问它。文件。我有问题,我第一次使用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]]; 
} 
+0

方法transfertImage将不得不将图像发送到SecondVieController ... – Swr79

+0

我刚刚创建了需要解决问题的片段。我也在上面改变了我的答案。请小心“委托”,你只是在错误的地方使用它。这不是你真正需要委托的过程。如果您有异步任务,并且委托人在进程结束时通知您,则更好的用法是。这里只是设置'UIImage'的简单调用。 –

+0

通常,我发布了它的工作方式。比较你的课程,看看我有什么不同。它关于编程和它的一个基本问题。这只是开始,所以要学会阅读代码。如果您已经遇到问题并且无法比较,则无法学习任何内容。 –

相关问题