为什么我的pushViewController不起作用

时间:2013-06-15 08:26:21

标签: ios pushviewcontroller

当我在tableView中点击一个元素时,会出现错误。我检查了所有的连接,但仍然无法解决。这是我的代码

#import "CGViewController.h"

#import "CGAppDelegate.h"

#import "sls.h"

#import "patientController.h"

@interface CGViewController ()

@end

@implementation CGViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Patients";
    CGAppDelegate *delegate =
    (CGAppDelegate *)[[UIApplication sharedApplication] delegate];
    patients = delegate.patients;
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add:)];
    self.navigationItem.leftBarButtonItem = addButtonItem;
    // Do any additional setup after loading the view, typically from a nib.
}

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

- (UITableViewCell *)tableView:(UITableView *)tv
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:@"cell"];
    if( nil == cell) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                                     reuseIdentifier:@"cell"];
    }
    if (indexPath.row < patients.count) {
        sls *thissls = [patients objectAtIndex:indexPath.row];
       cell.textLabel.text = thissls.patientName;
    }
        else {
            cell.textLabel.text = @"";
            cell.textLabel.textColor = [UIColor lightGrayColor];
        }
        return cell;
}


-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
    if ( editing != self.editing) {
        [super setEditing:editing animated:animated];
        [self.tableView setEditing:editing animated:animated];

        NSArray *indexes =
        [NSArray arrayWithObject:
         [NSIndexPath indexPathForRow:patients.count inSection:0]];
        if ( editing == YES ) {
            [self.tableView insertRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationLeft];
          }  else {
              [self.tableView deleteRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationLeft];
            }
    }

    [self.tableView reloadData];
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < patients.count ) {
        return UITableViewCellEditingStyleDelete;
    }
    else {
        return UITableViewCellEditingStyleNone;
    }
}

- (NSInteger)tableView:
(UITableView *)tv numberOfRowsInSection:
(NSInteger)section {
    NSInteger count = patients.count;
    if (self.editing) {
        count = count + 1;
    }
    return count;
}

-(void)tableView:(UITableView *)tv didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
    CGAppDelegate *delegate =
    (CGAppDelegate *)[[UIApplication sharedApplication] delegate];
    patientController *patient = [[patientController alloc] init];
    [delegate.navController pushViewController:patient animated: YES];

    [tv deselectRowAtIndexPath:indexPath animated: YES];
}
-(void) tableView:(UITableView *)tv
commitEditingStyle:(UITableViewCellEditingStyle)editing forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editing == UITableViewCellEditingStyleDelete) {
        [patients removeObjectAtIndex:indexPath.row];
        [tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                  withRowAnimation:UITableViewRowAnimationLeft];
    }
}
@end

如果您有任何疑问,如果您知道的话请尽快帮助您,因为我在截止日期前尽快帮助

提前致谢。

错误说Thread 1:signal SIGABRT

错误发生在main.m

#import <UIKit/UIKit.h>

#import "CGAppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([CGAppDelegate class]));
    }
}

它说错误就在这里

patientController *patient = [[patientController alloc] init];

这是我的patientController.h文件

#import <UIKit/UIKit.h>

@interface patientController : UIViewController {
    NSIndexPath *index;

    IBOutlet UIImageView * pictureView;
    IBOutlet UILabel * descriptionView1;
    IBOutlet UILabel * descriptionView2;
    IBOutlet UILabel * descriptionView3;
}

- (id)initwithIndexPath:(NSIndexPath *)indexPath;

@end

和我的.m文件

#import "patientController.h"

#import "CGAppDelegate.h"

#import "sls.h"

@interface patientController ()

@end

@implementation patientController

- (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 from its nib.
    CGAppDelegate *delegate =
    (CGAppDelegate *)[[UIApplication sharedApplication] delegate];
    sls *thissls = [delegate.patients objectAtIndex:index.row];
    self.title = thissls.patientName;
    self->descriptionView1.text = thissls.patientName;
    self->descriptionView2.text = thissls.surnameName;
    self->descriptionView3.text = thissls.dateOfBirth;
    self->pictureView.image = thissls.patientImage;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (id)initwithIndexPath:(NSIndexPath *)indexPath {
    if ( (self == [super init]) ) {
        index = indexPath;
    }
    return self;
}
@end

请任何人都可以帮忙

4 个答案:

答案 0 :(得分:0)

使用

 [self.navController pushViewController:patient animated: YES];

每个viewcontroller本身都有它的导航控制器引用,无需调用应用程序委托来调用它[导航控制器必须被初始化,所有的viewcontrollers必须被推入导航堆栈,直到这个一个让它工作]

因此,请使用此功能在appdelegate中设置导航控制器

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

//Note : this is an example set like this with your viewcontroller instance
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
    } else {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
    }
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    [self.window makeKeyAndVisible];
    return YES;
}

答案 1 :(得分:0)

如果您当前在navigationController中(为了推送新的ViewController,您必须这样做),那么只需调用这些行就可以推送新的控制器

-(void)tableView:(UITableView *)tv didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
   patientController *patient = [[patientController alloc] init];
   [self.navigationController pushViewController:patient animated: YES];
}

答案 2 :(得分:0)

要检查引发异常的位置,请在Xcode中的断点导航选项卡中单击左下角的加号按钮并添加Exceptions Breakpoint。然后从Xcode运行应用程序。下次引发异常时,就像你的情况一样,你的应用程序的执行将停止,Xcode将指向引发异常的代码行。

无论如何,为了让您了解navigationControllers的工作原理:

UINavigationController是一个UIViewController,它控制其他UIViewController实例的堆栈。据说这个其他视图控制器位于“导航堆栈”中。导航堆栈中的顶部项目是视图控制器,您可以在给定时间在屏幕上看到该视图。您可以在导航堆栈上推送(显示)或弹出(关闭)视图控制器。

UINavigationController初始化为'rootViewController',它将成为堆栈中的第一个视图控制器,因此当您将navigationController添加到屏幕上时(例如通过将其设置为appDelegate窗口的rootViewController),您首先看到它。导航堆栈上的每个UIViewController都通过属性“navigationController”引用它的navigationController。

在任何给定的点上,您都可以使用UINavigationController方法pushViewController:animated:popViewControllerAnimated:popToViewController:animated:popToRootViewControllerAnimated:在navigationStack中添加或删除viewControllers。当然,要查看此方法的结果,您应该在屏幕上显示navigationController。

有关更多信息,请参阅UINavigationController Class Reference

答案 3 :(得分:0)

使用:

[self.navigationController pushViewController:patient animated: YES];