2012-01-06 46 views
0

我試圖從其他viewControllers更改UIImageView,但我不知道爲什麼圖像不會更改!這裏是我的代碼:從另一個VI控制器更改UIImageView

#import "ViewController.h" 

@class ViewController; 
@interface CoverGallery : UIViewController { 

    ViewController *mainViewController; 
} 
@property (nonatomic,retain) ViewController *mainViewController; 
- (IBAction)img1; 

.M

- (IBAction)img1 { 

    mainViewController.coverArt.image = [UIImage imageNamed:@"coverDefault.png"]; 
    [self dismissModalViewControllerAnimated:YES]; 

} 

有一個叫封面藝術在MainViewController一個UIImageView,感謝

這裏是我的按鈕操作,其視圖之間切換:

ViewController(My First view) .h

@interface ViewController : UIViewController { 
ViewController *mainViewController; 

} 

@property (nonatomic, retain) ViewController *mainViewController; 


.m : 

@synthesize mainViewController; 


- (void) CoverGallery { 

    CoverGallery *gallery = [[CoverGallery alloc] initWithNibName:@"CoverGallery" bundle:nil]; 
    gallery.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self presentModalViewController:gallery animated:YES]; 

//here is the problem, compiler gives me the mainViewController is not the property of CoverGallery 
gallery. mainViewController = self; 
    [gallery release]; 

} 
+1

您是否檢查過[[UIImage imageNamed:@「coverDefault.png」]'是否實際返回有效圖像? – Till 2012-01-06 20:34:52

+0

你在哪裏設置你的'mainViewController'? – 2012-01-06 20:43:05

+1

是設置爲:[gallery.mainViewController = self];在展示圖庫視圖之前。 – 2012-01-06 20:59:04

回答

0

你如何初始化屬性mainViewController?當您呈現模態視圖時,您需要通過設置其屬性來將其設置爲MainViewController。 我想,你在MasterViewController這樣做:

CoverGallery *gallery = [[CoverGallery alloc]initWithNibName:@"CoverGallery" bundle:nil]; 
    gallery.masterViewContrller = self; //Set it here 
    [self presentModalViewController:gallery animated:YES]; 

我固定一個錯字.. 這:

ViewController *mainViewController; 

應該是:

MainViewController *mainViewController; 

,但你需要導入它在同一個頭文件中。

+0

gallery.masterViewController = self;我更新了代碼 – 2012-01-06 21:00:55

+0

第一個ViewController的名稱是什麼?它是否被命名爲ViewController?我認爲你的第一個viewController被命名爲MasterViewController,它是呈現模態視圖的。 – 2012-01-06 21:22:49

+0

你做了@property(nonatomic,retain)Viewcontroller * mainViewController;在你的頭部?並確保在.m中合成mainViewController。 – 2012-01-06 21:40:40