如何使用xcode 6从表视图控制器推送到集合视图控制器?

时间:2015-08-13 06:09:09

标签: ios objective-c xcode

  • 我正在使用Xcode 6来制作应用。我有两个表视图 控制器,Root和Second,现在我想从Second传递数据 表视图控制器到集合视图控制器。我添加了一个 segue但有一个例外。不知道我哪里出错了。

SecondViewController.h

 #import <UIKit/UIKit.h>



   @interface SecondTableViewController : UITableViewController



   @property(nonatomic, strong) NSString *businessName;



   @end














   code for CollectionViewController.m



   #import "CollectionViewController.h"

   #import "CollectionViewCell.h"



   @interface CollectionViewController ()



   @end



   @implementation CollectionViewController



   NSArray * favBC;

   NSArray * relToMe;

   NSArray * repByMe;

   NSArray * redForApp;

   NSArray * appBC;



   static NSString * const reuseIdentifier = @"collectionCell";



   - (void)viewDidLoad {

       [super viewDidLoad];



       // Uncomment the following line to preserve selection between presentations

       // self.clearsSelectionOnViewWillAppear = NO;



       favBC = [NSArray arrayWithObjects:@"IDEA1",@"NEW",@"IN VAL...",@"IN ESTI...",@"READY...",@"APPROV...",@"IT

CHAN ......&#34;,@&#34; UNDER ...&#34;,@&#34; IMPLEM&#34;,@&#34; CLOSED&#34;,nil]; < / p>

       relToMe = [NSArray arrayWithObjects:@"IDEA2",@"NEW",@"IN VAL...",@"IN ESTI...",@"READY...",@"APPROV...",@"IT

CHAN ......&#34;,@&#34; UNDER ...&#34;,@&#34; IMPLEM&#34;,@&#34; CLOSED&#34;,nil]; < / p>

       repByMe = [NSArray arrayWithObjects:@"IDEA3",@"NEW",@"IN VAL...",@"IN ESTI...",@"READY...",@"APPROV...",@"IT

CHAN ......&#34;,@&#34; UNDER ...&#34;,@&#34; IMPLEM&#34;,@&#34; CLOSED&#34;,nil]; < / p>

       redForApp = [NSArray arrayWithObjects:@"IDEA4",@"NEW",@"IN VAL...",@"IN ESTI...",@"READY...",@"APPROV...",@"IT

CHAN ......&#34;,@&#34; UNDER ...&#34;,@&#34; IMPLEM&#34;,@&#34; CLOSED&#34;,nil]; < / p>

       appBC = [NSArray arrayWithObjects:@"IDEA5",@"NEW",@"IN VAL...",@"IN ESTI...",@"READY...",@"APPROV...",@"IT

CHAN ......&#34;,@&#34; UNDER ...&#34;,@&#34; IMPLEM&#34;,@&#34; CLOSED&#34;,nil]; < / p>

       // Register cell classes

       [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];



       // Do any additional setup after loading the view.

   }



   - (void)didReceiveMemoryWarning {

       [super didReceiveMemoryWarning];

       // Dispose of any resources that can be recreated.

   }



   /*

   #pragma mark - Navigation



   // In a storyboard-based application, you will often want to do a little preparation before navigation

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

       // Get the new view controller using [segue destinationViewController].

       // Pass the selected object to the new view controller.

   }

   */



   #pragma mark <UICollectionViewDataSource>



   - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {



       return 1;

   }





   - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {



       if([_businessName2 isEqualToString:@"IDEA1"]){

           return [favBC count];



       }else if ([_businessName2 isEqualToString:@"IDEA2"]){

           return [relToMe count];



       }else if ([_businessName2 isEqualToString:@"IDEA3"]){

           return [repByMe count];



       }else if ([_businessName2 isEqualToString:@"IDEA4"]){

           return [redForApp count];



       }else if ([_businessName2 isEqualToString:@"IDEA5"]){

           return [appBC count];

       }



       return 0;

   }



   - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

       CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier

forIndexPath:indexPath];

       if([_businessName2 isEqualToString:@"IDEA1"]){

           cell.bcRequestLabel.text = [favBC objectAtIndex:indexPath.row];



       }else if ([_businessName2 isEqualToString:@"IDEA2"]){

           cell.bcRequestLabel.text = [favBC objectAtIndex:indexPath.row];



       }else if ([_businessName2 isEqualToString:@"IDEA3"]){

           cell.bcRequestLabel.text = [favBC objectAtIndex:indexPath.row];



       }else if ([_businessName2 isEqualToString:@"IDEA4"]){

           cell.bcRequestLabel.text = [favBC objectAtIndex:indexPath.row];



       }else if ([_businessName2 isEqualToString:@"IDEA5"]){

           cell.bcRequestLabel.text = [favBC objectAtIndex:indexPath.row];

       }



       // Configure the cell



       return cell;

   }



   #pragma mark <UICollectionViewDelegate>



   /*

   // Uncomment this method to specify if the specified item should be highlighted during tracking

   - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {

   return YES;

   }

   */



   /*

   // Uncomment this method to specify if the specified item should be selected

   - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {

       return YES;

   }

   */



   /*

   // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on

项目

   - (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {

   return NO;

   }



   - (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath

*)indexPath withSender:(id)sender {

   return NO;

   }



   - (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath

withSender:(id)发件人{

   }

   */



   @end


   #import "SecondTableViewController.h"

   #import "CollectionViewController.h"



   @interface SecondTableViewController ()



   @end



   @implementation SecondTableViewController



   NSArray * favBC;

   NSArray * relToMe;

   NSArray * repByMe;

   NSArray * redForApp;

   NSArray * appBC;



   - (void)viewDidLoad {

       [super viewDidLoad];



       favBC = [NSArray arrayWithObjects:@"IDEA1",@"NEW",@"IN VAL...",@"IN ESTI...",@"READY...",@"APPROV...",@"IT

CHAN ......&#34;,@&#34; UNDER ...&#34;,@&#34; IMPLEM&#34;,@&#34; CLOSED&#34;,nil]; < / p>

       relToMe = [NSArray arrayWithObjects:@"IDEA2",@"NEW",@"IN VAL...",@"IN ESTI...",@"READY...",@"APPROV...",@"IT

CHAN ......&#34;,@&#34; UNDER ...&#34;,@&#34; IMPLEM&#34;,@&#34; CLOSED&#34;,nil]; < / p>

       repByMe = [NSArray arrayWithObjects:@"IDEA3",@"NEW",@"IN VAL...",@"IN ESTI...",@"READY...",@"APPROV...",@"IT

CHAN ......&#34;,@&#34; UNDER ...&#34;,@&#34; IMPLEM&#34;,@&#34; CLOSED&#34;,nil]; < / p>

       redForApp = [NSArray arrayWithObjects:@"IDEA4",@"NEW",@"IN VAL...",@"IN ESTI...",@"READY...",@"APPROV...",@"IT

CHAN ......&#34;,@&#34; UNDER ...&#34;,@&#34; IMPLEM&#34;,@&#34; CLOSED&#34;,nil]; < / p>

       appBC = [NSArray arrayWithObjects:@"IDEA5",@"NEW",@"IN VAL...",@"IN ESTI...",@"READY...",@"APPROV...",@"IT

CHAN ......&#34;,@&#34; UNDER ...&#34;,@&#34; IMPLEM&#34;,@&#34; CLOSED&#34;,nil]; < / p>

   }



   - (void)didReceiveMemoryWarning {

       [super didReceiveMemoryWarning];

       // Dispose of any resources that can be recreated.

   }



   #pragma mark - Table view data source



   - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {



       return 1;

   }



   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

   {



       if([_businessName isEqualToString:@"Favorite BC"]){

           return [favBC count];



       }else if ([_businessName isEqualToString:@"Related to me"]){

           return [relToMe count];



       }else if ([_businessName isEqualToString:@"Reported by me"]){

           return [repByMe count];



       }else if ([_businessName isEqualToString:@"Ready for approval"]){

           return [redForApp count];



       }else if ([_businessName isEqualToString:@"Approved BC"]){

           return [appBC count];

       }



       return 0;



   }





   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

   {

       static NSString *simpleTableIdentifier = @"Business2Cell";



       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier

forIndexPath:indexPath];

       if(cell == nil){

           cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault

reuseIdentifier:simpleTableIdentifier];

       }

       if([_businessName isEqualToString:@"Favorite BC"]){

           cell.textLabel.text = [favBC objectAtIndex:indexPath.row];



       }else if ([_businessName isEqualToString:@"Related to me"]){

           cell.textLabel.text = [relToMe objectAtIndex:indexPath.row];



       }else if ([_businessName isEqualToString:@"Reported by me"]){

           cell.textLabel.text = [repByMe objectAtIndex:indexPath.row];



       }else if ([_businessName isEqualToString:@"Ready for approval"]){

           cell.textLabel.text = [redForApp objectAtIndex:indexPath.row];



       }else if ([_businessName isEqualToString:@"Approved BC"]){

           cell.textLabel.text = [appBC objectAtIndex:indexPath.row];



       }





       // Configure the cell...



       return cell;

   }





   /*

   // Override to support conditional editing of the table view.

   - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

       // Return NO if you do not want the specified item to be editable.

       return YES;

   }

   */



   /*

   // Override to support editing the table view.

   - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath {

       if (editingStyle == UITableViewCellEditingStyleDelete) {

           // Delete the row from the data source

           [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

       } else if (editingStyle == UITableViewCellEditingStyleInsert) {

           // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view

       }   

   }

   */



   /*

   // Override to support rearranging the table view.

   - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath

toIndexPath:(NSIndexPath *)toIndexPath {

   }

   */



   /*

   // Override to support conditional rearranging of the table view.

   - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {

       // Return NO if you do not want the item to be re-orderable.

       return YES;

   }

   */





   #pragma mark - Navigation



   // In a storyboard-based application, you will often want to do a little preparation before navigation

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

       // Get the new view controller using [segue destinationViewController].

       // Pass the selected object to the new view controller.



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

       {

           NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];



           CollectionViewController *destViewController = segue.destinationViewController;



           if([_businessName isEqualToString:@"Favorite BC"]){

               destViewController.businessName2 = [favBC objectAtIndex:indexPath.row];



           }else if ([_businessName isEqualToString:@"Related to me"]){

               destViewController.businessName2 = [relToMe objectAtIndex:indexPath.row];



           }else if ([_businessName isEqualToString:@"Reported by me"]){

               destViewController.businessName2 = [repByMe objectAtIndex:indexPath.row];



           }else if ([_businessName isEqualToString:@"Ready for approval"]){

               destViewController.businessName2 = [redForApp objectAtIndex:indexPath.row];



           }else if ([_businessName isEqualToString:@"Approved BC"]){

               destViewController.businessName2 = [appBC objectAtIndex:indexPath.row];

           }



           destViewController.title = destViewController.businessName2;



       }

   }





   @end



   code for CollectionViewController.h





   #import <UIKit/UIKit.h>



   @interface CollectionViewController : UICollectionViewController



   @property(nonatomic, strong) NSString *businessName2;



   @end


   code for CollectionViewCell.h



   #import <UIKit/UIKit.h>



   @interface CollectionViewCell : UICollectionViewCell



   @property (weak, nonatomic) IBOutlet UILabel *bcRequestLabel;



   @end



   code for CollectionViewCell.m



   #import "CollectionViewCell.h"



   @implementation CollectionViewCell



   @end





   <b>and the error:<b>



       2015-08-13 15:45:37.500 WACProject2[4049:82472] -[UICollectionViewCell bcRequestLabel]: unrecognized selector sent to instance 0x7fb031f59ab0

   2015-08-13 15:45:37.503 WACProject2[4049:82472] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:

&#39; - [UICollectionViewCell bcRequestLabel]:无法识别的选择器已发送    例如0x7fb031f59ab0&#39;

   *** First throw call stack:

   (

   0   CoreFoundation                      0x0000000110722f35 __exceptionPreprocess + 165

   1   libobjc.A.dylib                     0x00000001103bbbb7 objc_exception_throw + 45

   2   CoreFoundation                      0x000000011072a04d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205

   3   CoreFoundation                      0x000000011068227c ___forwarding___ + 988

   4   CoreFoundation                      0x0000000110681e18 _CF_forwarding_prep_0 + 120

   5   WACProject2                         0x000000010fe8be7b -[CollectionViewController collectionView:cellForItemAtIndexPath:] + 187

   6   UIKit                               0x000000011113d41b -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:]

+ 244

   7   UIKit                               0x000000011113eb54 -[UICollectionView _updateVisibleCellsNow:] + 3445

   8   UIKit                               0x0000000111142801 -[UICollectionView layoutSubviews] + 243

   9   UIKit                               0x0000000110b88973 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521

   10  QuartzCore                          0x00000001143fdde8 -[CALayer layoutSublayers] + 150

   11  QuartzCore                          0x00000001143f2a0e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380

   12  QuartzCore                          0x00000001143f287e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24

   13  QuartzCore                          0x000000011436063e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242

   14  QuartzCore                          0x000000011436174a _ZN2CA11Transaction6commitEv + 390

   15  QuartzCore                          0x0000000114361db5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89

   16  CoreFoundation                      0x0000000110657dc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23

   17  CoreFoundation                      0x0000000110657d20 __CFRunLoopDoObservers + 368

   18  CoreFoundation                      0x000000011064db53 __CFRunLoopRun + 1123

   19  CoreFoundation                      0x000000011064d486 CFRunLoopRunSpecific + 470

   20  GraphicsServices                    0x0000000113cf19f0 GSEventRunModal + 161

   21  UIKit                               0x0000000110b0f420 UIApplicationMain + 1282

   22  WACProject2                         0x000000010fe8c3e3 main + 115

   23  libdyld.dylib                       0x0000000112cb2145 start + 1

   24  ???                                 0x0000000000000001 0x0 + 1

   )

   libc++abi.dylib: terminating with uncaught exception of type NSException

3 个答案:

答案 0 :(得分:2)

-[UICollectionViewCell bcRequestLabel] unrecognized selector sent to instance…

您正在访问bcRequestLabel的{​​{1}},该UICollectionViewCell不存在。

确保从故事板中的集合视图单元格中正确设置自定义类。

答案 1 :(得分:1)

错误是因为您指示目标viewController类是CollectionViewController,但CollectionViewController未实现businessName。您应该指明实现businessName的自定义类,而不是CollectionViewController。

答案 2 :(得分:1)

在故事板中指定目标视图控制器CollectionViewController的类名,如enter image description here