我是xcode的新手,所以我在这里学习,我可能会完全错误,但这是我的情景。我有一个开放视图控制器的故事板,带有一个按钮,进入导航控制器,使用表视图从一个mysql数据库加载我的娱乐列表(这很好)一个项目从它显示的表视图转到详细信息视图但没有发生任何事情,调试器中没有错误。我做错了什么或者我以错误的方式解决了这个问题。我认为问题与ENTERTAILMENTLISTING.M中有关didSelectRowAtIndexPath内容的部分有关,我已经包含了目前为止的所有内容。
故事板
APPDELEGATE.H
//
// AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
APPDELEGATE.M
//
// AppDelegate.m
#import "AppDelegate.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
ENTERTAINMENTLISTING.H
//
// EntertainmentListingViewController.h
#import <UIKit/UIKit.h>
@interface EntertainmentListingViewController : UIViewController{
IBOutlet UITableView *mainTableView;
NSArray *events;
NSMutableData *data;
}
@end
ENTERTAINMENTLISTING.M
//
// EntertainmentListingViewController.m
#import "EntertainmentListingViewController.h"
#import "EntertainmentDetailsViewController.h"
@interface EntertainmentListingViewController ()
@end
@implementation EntertainmentListingViewController
- (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.
// VIEW TITLE
self.title = @"Entertainment";
// SHOW NETWORK ACTIVITY INDICATOR
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
//GET DATA URL
NSURL *url = [NSURL URLWithString:@"http://mydomain.com/myfile.php"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
//START CODE FOR TALBE VIEW
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
data = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
[data appendData:theData];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//PROCESS JSON DATA HERE
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
events = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
[mainTableView reloadData];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// WASNT ABLE TO CONNECT INTERNET THROW ERROR
UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"No Connection To The Internet Is Available" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[errorView show];
// TURN OFF NETWORk ACTIVITY INDICATOR
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (int) numberOfSectionsInTableView: (UITableView *)tableView
{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [events count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)IndexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:@"MainCell"];
}
NSURL *url = [NSURL URLWithString: [[events objectAtIndex:IndexPath.row] objectForKey:@"eImg"]];
NSData *idata = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:idata];
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
cell.imageView.image = imgView.image;
cell.textLabel.text = [[events objectAtIndex:IndexPath.row] objectForKey:@"eName"];
cell.detailTextLabel.text = [[events objectAtIndex:IndexPath.row] objectForKey:@"date_string"];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
EntertainmentDetailsViewController *entertainmentdetailsViewController = [[EntertainmentDetailsViewController alloc]
initWithNibName:@"EntertainmentDetailsViewController" bundle:nil];
entertainmentdetailsViewController.title = [[events objectAtIndex:indexPath.row] objectForKey:@"eName"];
entertainmentdetailsViewController.entertainmentArticle = [events objectAtIndex:indexPath.row];
[self.navigationController pushViewController:entertainmentdetailsViewController animated:YES];
// NOTHING HAPPENING HAS TO DO WITH THIS AREA I THINK
NSLog(@"Navigation Cnntroller %@",self.navigationController);
NSLog(@"Events COntroller %@", entertainmentdetailsViewController);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
ENTERTAINMENTDETAILS.H
//
// EntertainmentDetailsViewController.h
#import <UIKit/UIKit.h>
@interface EntertainmentDetailsViewController : UIViewController{
NSDictionary *entertainmentArticle;
IBOutlet UILabel *titleLabel;
IBOutlet UILabel *timeLabel;
IBOutlet UITextView *descTextView;
}
@property (nonatomic, copy) NSDictionary *entertainmentArticle;
@end
ENTERTAINMENTDETAILS.M
//
// EntertainmentDetailsViewController.m
#import "EntertainmentDetailsViewController.h"
@interface EntertainmentDetailsViewController ()
@end
@implementation EntertainmentDetailsViewController
@synthesize entertainmentArticle;
- (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 = [entertainmentArticle objectForKey:@"eName"];
timeLabel.text = [entertainmentArticle objectForKey:@"date_string"];
descTextView.text = [entertainmentArticle objectForKey:@"eDetails"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
答案 0 :(得分:2)
您忘记了委托和数据源。如果你使用uitableviewcontroller,所有都被连接起来,如果你将tableview拖到uiviewcontroller,你必须做几件事:
编辑此行:
@interface EntertainmentListingViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
按住Ctrl键并将表格拖动到视图控制器下方的黄色框中。设置为委托,再次拖动并设置为源。
答案 1 :(得分:0)
将委托指定为self并设置断点以查找控件是否到达委托方法
答案 2 :(得分:0)
这里EntertainmentListingViewController是UIViewController @interface EntertainmentListingViewController : UIViewController
的子类。所以你不能使用[self.navigationController pushViewController:entertainmentdetailsViewController animated:YES];
API。您可以使用[self presentModalViewController:entertainmentdetailsViewController animated:YES];