2013-10-12 65 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

通常,我發佈了它的工作方式。比較你的課程,看看我有什麼不同。它關於編程和它的一個基本問題。這只是開始,所以要學會閱讀代碼。如果您已經遇到問題並且無法比較,則無法學習任何內容。 –

相關問題