0

在目前我有以下的代碼如下所示:UICollectionViewCell定製Segue公司的ViewController連接錯誤

GroupsViewController.m

#import "GroupsViewController.h" 
#import "GroupsHomeViewController.h" 
#import "CustomCell.h" 

@interface GroupsViewController() 
{ 
    NSArray *arrayOfImages; 
    NSArray *arrayOfDescriptions; 
    NSString * _titleForNextVC; 
} 

@end 

@implementation GroupsViewController 
{ 
    NSString *reuseIdentifier; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [[self GroupsCollectionView]setDataSource:self]; 
    [[self GroupsCollectionView]setDelegate:self]; 
    reuseIdentifier= @"SmallIcon"; 

    arrayOfImages = [[NSArray alloc]initWithObjects:@"A.png",@"B.png",@"C.png",nil]; 

    arrayOfDescriptions = [[NSArray alloc]initWithObjects:@"A",@"B",@"C",nil]; 
} 

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 
{ 
    return 1; 
} 

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    return [arrayOfDescriptions count]; 
} 


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 
    [[cell IconImage]setImage:[UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]]]; 
    [[cell IconLabel]setText:[arrayOfDescriptions objectAtIndex:indexPath.item]]; 
    return cell; 
} 

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomCell *cell = (CustomCell *) [self collectionView:collectionView cellForItemAtIndexPath:indexPath]; 
    _titleForNextVC = cell.IconLabel.text; 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

    if ([segue.identifier isEqualToString:@"GroupsHomeSegue"]) { 

     GroupsHomeViewController *vc = (GroupsHomeViewController *)segue.destinationViewController; 
     vc.titleText = _titleForNextVC; 
    } 
} 

- (void)setTitleText:(NSString *)titleText { 

    _titleText = _titleForNextVC; 

    // Set Title of your ViewController 
    self.title = _titleText; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    //Dispose of any resources that can be recreated. 
} 

// Toggle View Button 
- (IBAction)cellToggleAction:(id)sender { 

    if([reuseIdentifier isEqualToString:@"SmallIcon"]){ 
     [email protected]"ListView"; 
     [sender setImage:[UIImage imageNamed:@"LargeIcon"]]; 
    } 
    else if 
     ([reuseIdentifier isEqualToString:@"ListView"]){ 
     [email protected]"LargeIcon"; 
     [sender setImage:[UIImage imageNamed:@"SmallIcon"]]; 
    } 
    else if 
     ([reuseIdentifier isEqualToString:@"LargeIcon"]){ 
     [email protected]"SmallIcon"; 
     [sender setImage:[UIImage imageNamed:@"ListView"]]; 
    } 

    [self.GroupsCollectionView reloadData]; 
} 

//Toggled Cell Sizes 
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 

    CGSize cellSize; 

    if([reuseIdentifier isEqualToString:@"SmallIcon"]) 
     cellSize = CGSizeMake(100, 130); 
    else if 
     ([reuseIdentifier isEqualToString:@"ListView"]) 
     cellSize = CGSizeMake(320, 50); 
    else if 
     ([reuseIdentifier isEqualToString:@"LargeIcon"]) 
     cellSize = CGSizeMake(320, 360); 

    return cellSize; 
} 

@end 

GroupsHomeViewController.m

#import "GroupsHomeViewController.h" 

@interface GroupsHomeViewController() 

@end 

@implementation GroupsHomeViewController 

-(void)viewDidLoad{ 
    [super viewDidLoad]; 

} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

好這個問題似乎是我無法通過我的自定義搜索獲得新的ViewController,一旦我點擊了我的UiCollectionViewcells之一。基本上,我點擊我的UICollectionView中的一個單元格,標題消失,但它什麼也沒有做。它應該打開GroupsHomeViewController並將View Controller的標題設置爲放置在剛剛單擊的單元格中的標籤。我甚至無法看到我的當前標題是否會起作用,因爲我無法顯示我的GroupsHomeViewController

我假設我錯過了一行代碼,但我很努力地找出它可能是因爲沒有收到任何錯誤消息。

此外,我只想指出,我是新來的,只是在過去一個月左右的空閒時間開發我的應用程序。所以,如果你能幫助我解決這個問題,我將不勝感激,並且我事先感謝你對我可能錯過的任何方向。

+1

您會得到更好的如果你的問題是*最小*,完整和可驗證的答案(http://stackoverflow.com/help/mcve)。也就是說,給我們最少量的代碼來重現你的問題,而不是給我們完整的課程。 – EmilioPelaez

+0

你是如何添加賽格? – CaptJak

+0

我控制從StoryView模式下ViewController頂部的GroupsViewController Orange/Yellow按鈕中點擊,然後將它放到我的第二個空ViewController上,例如GroupsHomeViewController。 –

回答

1

所以問題是你需要保存你試圖傳遞給屬性中下一個視圖控制器的「what ever」。在你的情況,我認爲你有_titleForNextVC

下一個是你需要命名賽格瑞在故事板如「GroupsHomeSegue」,那麼你可以

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomCell *cell = (CustomCell *) [self collectionView:collectionView cellForItemAtIndexPath:indexPath]; 
    _titleForNextVC = cell.IconLabel.text; 

    [self performSegueWithIdentifier:@"GroupsHomeSegue" sender:self]; 
} 

然後它會工作

+0

應用你的建議代碼現在實際上給了我一個反應/錯誤代碼現在謝謝你,我必須更近一步。現在唯一的問題是我選擇一個單元格後收到以下錯誤。 2016-02-08 22:44:00.935 Octane Hub [553:10961] - [UITabBarController setTitleText:]:無法識別的選擇器已發送至實例0x7fefb3754ba0 2016-02-08 22:44:00.942 Octane Hub [553:10961] ** *終止應用程序,由於未捕獲的異常'NSInvalidArgumentException',原因:' - [UITabBarController setTitleText:]:無法識別的選擇器發送到實例0x7fefb3754ba0' –

+0

你能標記我的答案嗎?剩餘的錯誤,我們可以找出,如果你發佈一個不同的問題 –

+0

你有目標viewController中的UITabBarController你要? –