试图将ViewController与赞助商的UITableView转移到另一个ViewController

时间:2015-10-13 19:19:03

标签: ios objective-c iphone uiviewcontroller segue

我还是iOS编程新手,我正在尝试实施一个应用程序,为参加周末漫长黑客马拉松的学生显示信息。我目前正在制作一张充满赞助商名单的桌子,当您点击一个赞助商披露按钮时,我希望它能够转到另一个页面,该页面只显示赞助商的名称和包含其网站的网页视图。

我以为我正确设置了prepareForSegue方法,但是当我尝试在iOS模拟器中进行选择时没有任何反应。有人可以看看我的代码,并告诉我,如果我错过了什么?

StoryBoard Screenshot iOS Simulator Screenshot

SponsorMasterViewController .h文件从此处开始

#import <UIKit/UIKit.h>
#import "SponsorDetailViewController.h"


@interface SponsorMasterViewController : UITableViewController <UITableViewDataSource,
    UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *tableOfSponsers;

@end

SponsorMasterViewController .m文件从此处开始

#import "SponsorMasterViewController.h"

@interface SponsorMasterViewController ()

@end

@implementation SponsorMasterViewController {

    NSArray *allSponsors;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    Sponsor *sponUber = [[Sponsor alloc] init];
    sponUber.name = @"Über Operations LLC";
    sponUber.url = @"https://www.uberops.com";

    Sponsor *sponStar = [[Sponsor alloc] init];
    sponStar.name = @"Starbucks";
    sponStar.url = @"http://www.starbucks.com";

    Sponsor *sponRen = [[Sponsor alloc] init];
    sponRen.name = @"Renegade Runner";
    sponRen.url = @"https://fsudelivery.com";

    Sponsor *sponOEI = [[Sponsor alloc] init];
    sponOEI.name = @"Office of Entrepreneurship";
    sponOEI.url = @"http://sga.fsu.edu/oei.shtml";

    Sponsor *sponJim = [[Sponsor alloc] init];
    sponJim.name = @"The Jim Morgan Institute for Global Entrepreneurship";
    sponJim.url = @"http://business.fsu.edu/jmi";

    Sponsor *sponMail = [[Sponsor alloc] init];
    sponMail.name = @"MailChimp";
    sponMail.url = @"http://mailchimp.com";

    Sponsor *sponGen = [[Sponsor alloc] init];
    sponGen.name = @"Genivia";
    sponGen.url = @"http://www.genivia.com";

    Sponsor *sponFL = [[Sponsor alloc] init];
    sponFL.name = @"FLorida IT Career";
    sponFL.url = @"https://fitc.cci.fsu.edu";

    Sponsor *sponDOMI = [[Sponsor alloc] init];
    sponDOMI.name = @"domiventures";
    sponDOMI.url = @"http://www.domistation.com";

    Sponsor *sponCode = [[Sponsor alloc] init];
    sponCode.name = @"Code School";
    sponCode.url = @"https://www.codeschool.com";

    Sponsor *sponIonic = [[Sponsor alloc] init];
    sponIonic.name = @"Ionic Security";
    sponIonic.url = @"https://www.ionic.com";

    allSponsors = [NSArray arrayWithObjects:sponUber, sponStar, sponRen, sponOEI,
                   sponJim, sponMail, sponGen, sponFL, sponDOMI, sponCode,
                   sponIonic, nil];
}

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

#pragma mark - TableView Functions

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //Determine number of sections in my table
    return 1;
}

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

    return [allSponsors count];
}

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

    static NSString *simpleTableIdentifier = @"SponserCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if ( cell == nil ) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    Sponsor *company = [allSponsors objectAtIndex:indexPath.row];
    cell.textLabel.text = company.name;
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

    return cell;
}

#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 {

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

        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

        SponsorDetailViewController *sponsorDetailController = (SponsorDetailViewController *)segue.destinationViewController;


        sponsorDetailController.spon = [allSponsors objectAtIndex:indexPath.row];
    }

    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
@end

SponsorDetailViewController .h文件从此处开始

#import <UIKit/UIKit.h>
#import "Sponsor.h"

@interface SponsorDetailViewController : UIViewController

@property (nonatomic, strong) IBOutlet UILabel *sponsorName;
@property (nonatomic, strong) IBOutlet UIWebView *webView;

@property (nonatomic, strong) Sponsor *spon;

@end

SponsorDetailViewController .m文件从此处开始

#import "SponsorDetailViewController.h"

@interface SponsorDetailViewController ()

@end

@implementation SponsorDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //Set the Sponse Label
    [self.sponsorName setText:self.spon.name];

    //Process sponsor url
    NSURL *sponsorURL = [NSURL URLWithString:self.spon.url];
    NSURLRequest *request = [NSURLRequest requestWithURL:sponsorURL];

    //fit contents of website inside my webview
    self.webView.scalesPageToFit = YES;
    [self.webView loadRequest:request];

}

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

@end

2 个答案:

答案 0 :(得分:0)

我看到你已经在故事板中定义了从masterViewController到DetailViewController的push segue。

要执行segue,您需要实现UITableView Delegate方法,特别是tableView:didSelectRowAtIndexPath:

PrepareForSegue是对segues的准备,它没有执行segues。

但是,当您点击“详细信息”按钮时,它不会发生,但如果您按照此实现方式点击该单元格。

答案 1 :(得分:0)

你应该实施

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

类似

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"sponsorSegue" sender:[self.allSponsors objectAtIndexPath: indexPath]];
}

然后改变你准备segue sponsorDetailController.spon = sender;