TableViewController按钮连接

时间:2014-04-19 13:16:32

标签: ios uitableview

我正在为学校制作一个项目,我遇到了表视图控制器的问题,我想添加一个按钮将它链接到另一个页面,真的是它,所以我有一个7个表格单元的列表连接到详细信息视图控制器,从那里我想添加一个按钮,将其链接到另一个页面,但7个表格单元格中的每一个都需要转到不同的页面,对不起,我不是很擅长解释事情,但如果任何人可以帮助我真的很感激。

TableViewController.m

#import "tableViewController.h"
#import "TableCell.h"
#import "DetailViewController.h"

@interface tableViewController ()

@end

@implementation tableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;

_Title =@[@"Centrum Advance", @"Elite Whey Protein", @"USP LABS Jack3d", @"NitroCore 24", @"PHD Synergy ISO 7", @"REFLEX One Stop", @"MET-Rx G.C.M.O"];

_Images=@[@"1C.jpg",@"2C.jpg",@"3C.jpg",@"4C.jpg",@"5C.jpg",@"6C.jpg",@"mets.jpg",];

_Review=@[@"Not Reviewed",@"'I'm a big fan of whey protein becasue the dosing is simple, easy to consume, and usually a lot cheaper than pills. I use to use the regular old GNC 100% whey protein which was good to take after a workout, but I really wanted more from my supplements. Proto Whey had a lot of great reviews and I have to say, it worked pretty well for me.'",@"'Wow. This stuff is amazing. No fillers, the taste (lemon-lime) isn't overwhelming, and I can work out solid for 1.5 hours and still feel like a beast. I've taken NO-Xplode and Black Powder, and I find that Jack3d provides more energy, while something like Black Powder gives me more pump. This is why I mix a scoop of Black Powder and a scoop of Jack3d and get totally effed at the gym.I've come to notice that if I take jack3d later in the day, it really messes with my sleep, and I contribute that to the DMAA in it, which is a stimulant. I just finished a bottle and I notice my tolerance was going up. On days that I was taking just jack3d, since I like to mix it up, I moved up to 2 scoops towards the end of the bottle. So all in all, awesome for energy in the gym, only somewhat decent regarding pump. But I still give it a solid 9. I have yet to try out 1.M.R, which I will be getting next week, and can find out then which is better for me.BTW I didn't get a 'crash' that so many people speak of, this could be that maybe I am not just prone to the type of thing, and that I work out after I get off work, so 'crashing' really doesn't matter for me.'",@"Not Reviewed",@"'I had the double chocolate flavor and it was the BEST tasting stuff I've ever had. It was so good, sometimes I just took a serviing for breakfast if I was running short on time. The effectiveness was good too, and after finishing a tub of it, I noticed just about all of my lifts increased. I gained some great vascularity in my bi's, and my chest strength exploded. Honestly, for all of you who want to spend a little money and get a nice supplement...this is your winner. I am definitely gonna use this for a while until I get sick of the taste or see a new up-and-coming product. Only reason I give value a 5 is because I paid about $40 for the 2LB tub back in January.'",@"Not Reviewed",@"Not Reviewed",];

_Size=@[@"1.2KG",@"3KG",@"2.7KG",@"3.9KG",@"5KG",@"6.4KG",@"0.5KG",];

_Does=@[@"The perfect whey protein to aid weight loss or pack on lean muscle mass",@"Add some         serious muscle to your body without the fat!",@"24g of protein per scoop for just 112 calories and no sugars!",@"Premium Whey Isolate",@"Minimal carbohydrates and fats",@"The Only Way To Enjoy Whey Protein",@"27g of protein with each serving",];

}

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

#pragma mark - Table view data source

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

// Return the number of sections.
return 1;
}

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

// Return the number of rows in the section.
return _Title.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TableCell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

// Configure the cell...

   int row = [indexPath row];

   cell.TitleLabel.text = _Title[row];
   cell.ThumbImage.image = [UIImage imageNamed:_Images[row]];
cell.Review1.text = _Review[row];
cell.Size1.text = _Size[row];
cell.Does1.text = _Does[row];

   return cell;
}

 -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
   if([[segue identifier] isEqualToString:@"ShowDetails"]) {
       DetailViewController *detailviewcontroller = [segue destinationViewController];

    NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];

    int row = [myIndexPath row];
   detailviewcontroller.DetailModal = @[_Title[row],_Images[row]];
   }
}
@end

DetailViewController.m

 #import "DetailViewController.h"

 @interface DetailViewController ()

@end

 @implementation DetailViewController

- (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.
_TitleLabel.text = _DetailModal[0];
_ThumbImage.image = [UIImage imageNamed:_DetailModal [1]];

self.navigationItem.title = _DetailModal[0];

}

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

 @end

表格cell.m为空,.h用于IBOutlets

1 个答案:

答案 0 :(得分:0)

在DetailViewController上创建一个公共@property,您可以在prepareForSegue:sender: TableViewController中设置它,以在每个DetailViewController中设置按钮的目标。

// DetailViewController.h
@interface DetailViewController    
@property NSInteger nextPageId;  // or whatever

// TableViewController.m
// prepareForSegue...
detailviewcontroller.nextPageId = myIndexPath.row;  // something like this

// DetailViewController.m
// viewDidLoad...
[myButton addTarget:self action:@selector(myButtonHandlerMethod) forControlEvents:UIControlEventTouchUpInside];

-(void)myButtonHandlerMethod {
   // code to present the next "page" based on self.nextPageId
}

方法addTarget:action:forControlEvents:设置了当用户点按按钮时要调用的另一个方法myButtonHandlerMethod。如果UIButton位于DetailViewController Storyboard中,则创建一个IBOutlet属性并将该按钮引用为self.myButton