基本的iOS问题

时间:2012-11-29 14:46:44

标签: objective-c ios xcode tableview

我正在尝试从Apple开发者计划中完成“您的第二个iOS应用程序”教程。这是一个基本的tableView应用程序。我的问题是应用程序正在成功构建而没有警告,但是我无法从主视图中获取细节视图。我复制并粘贴了segue标识符和apple提供的代码。 segue正在使用推送,我删除了它并再次尝试了几次。我正在模拟器中测试应用程序。

  1. 如何确定segue是否正常工作?

  2. 每次我将代码从Xcode复制/粘贴到堆栈溢出问题文本区域时,我会在底部收到警告,说代码必须缩进4个空格???这是否意味着我必须逐行缩进代码?我做了控制+ k并粘贴在突出显示的区域,但我仍然收到警告??

  3. 当运行模拟器并查看它时,我试图通过点击它来使用披露指示器,我是否必须推送一些特别的东西,如control = click或command = click等?

  4. 以下是BirdsMasterViewController.m文件的代码:

                //
                //  BirdsMasterViewController.m
                //  BirdWatching
                //
                //  Created by David Hall on 11/13/12.
                //  Copyright (c) 2012 David Hall. All rights reserved.
                //
    
                #import "BirdsMasterViewController.h"
    
                #import "BirdsDetailViewController.h"
    
                #import "BirdSightingDataController.h"
    
                #import "BirdSighting.h"
    
                /*
                @interface BirdsMasterViewController () {
                    NSMutableArray *_objects;
                }
                @end
                */
                @implementation BirdsMasterViewController
    
                - (void)awakeFromNib
                {
                    [super awakeFromNib];
    
                    self.dataController = [[BirdSightingDataController alloc] init];
                }
    
                - (void)viewDidLoad
                {
                    [super viewDidLoad];
                    // Do any additional setup after loading the view, typically from a nib.
                /*
                    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    
                    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
                    self.navigationItem.rightBarButtonItem = addButton;
                */
                 }
    
                - (void)viewDidUnload
                {
                    [super viewDidUnload];
                    // Release any retained subviews of the main view.
                }
    
                - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
                {
                    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
                }
    
                /*- (void)insertNewObject:(id)sender
                {
                    if (!_objects) {
                        _objects = [[NSMutableArray alloc] init];
                    }
                    [_objects insertObject:[NSDate date] atIndex:0];
                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
                    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                }
                */
                #pragma mark - Table View
    
                - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
                {
                    return 1;
                }
    
                - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
                {
                    return [self.dataController countOfList];
                }
    
                - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
                {
                    static NSString *CellIdentifier = @"BirdSightingCell";
    
                    static NSDateFormatter *formatter = nil;
                    if (formatter == nil)
                    {
                        formatter = [[NSDateFormatter alloc] init];
                        [formatter setDateStyle:NSDateFormatterMediumStyle];
                    }
    
                    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                    BirdSighting *sightingAtIndex = [self.dataController objectInListAtIndex:indexPath.row];
                    [[cell textLabel] setText:sightingAtIndex.name];
                    [[cell detailTextLabel] setText:[formatter stringFromDate:(NSDate *)sightingAtIndex.date]];
    
    
                    return cell;
                }
    
                - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
                {
                    // Return NO if you do not want the specified item to be editable.
                    return NO;
                }
    
                /*- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
                {
                    if (editingStyle == UITableViewCellEditingStyleDelete) {
                        [_objects removeObjectAtIndex:indexPath.row];
                        [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;
                }
                */
    
                - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
                {
                    if ([[segue identifier] isEqualToString:@"ShowSightingDetails"]) {
                        BirdsDetailViewController *detailViewController = [segue destinationViewController];
                        detailViewController.sighting = [self.dataController objectInListAtIndex:[self.tableView indexPathForSelectedRow].row];
    
                    }
                }
    
                @end
    

    这是BirdsDetailViewController.m的代码

            //
            //  BirdSightingDataController.m
            //  BirdWatching
            //
            //  Created by David Hall on 11/25/12.
            //  Copyright (c) 2012 David Hall. All rights reserved.
            //
    
            #import "BirdSightingDataController.h"
            #import "BirdSighting.h"
    
            @interface BirdSightingDataController ()
    
            - (void)initializeDefaultDataList;
    
            @end
    
            @implementation BirdSightingDataController
    
            - (void)initializeDefaultDataList
            {
                NSMutableArray *sightingList = [[NSMutableArray alloc] init];
    
                self.masterBirdSightingList = sightingList;
    
                BirdSighting *sighting;
    
                NSDate *today = [NSDate date];
    
                sighting = [[BirdSighting alloc] initWithName:@"Pigeon" location:@"Everywhere" date:today];
    
                [self addBirdSightingWithSighting:sighting];
    
            }
    
            - (void)setMasterBirdSightingList:(NSMutableArray *)newList
            {
                if (_masterBirdSightingList != newList)
                {
                    _masterBirdSightingList = [newList mutableCopy];
                }
    
            }
    
            - (id)init
            {
                if (self = [super init])
                {
                    [self initializeDefaultDataList];
    
                    return self;
                }
    
                return nil;
            }
    
            - (NSUInteger)countOfList
            {
                return [self.masterBirdSightingList count];
            }
    
            - (BirdSighting *)objectInListAtIndex:(NSUInteger)theIndex
            {
                return [self.masterBirdSightingList objectAtIndex:theIndex];
            }
    
            - (void)addBirdSightingWithSighting:(BirdSighting *)sighting
            {
                [self.masterBirdSightingList addObject:sighting];
            }
    
    
    
    
    
    
    
    
    
            @end
    

    David Hall

3 个答案:

答案 0 :(得分:0)

  1. 它正常工作。但是如果不知道你在做什么,很难给你一个答案。

  2. 突出显示文本框中的代码,然后单击此按钮。

    enter image description here

    它将为您正确缩进代码。

  3. 我无法理解这个问题。

  4. 已编辑添加

    (看到OP寄给我的项目副本后)

    您的segue没有正确接线。

    segue应该从单元格转到下一个视图控制器,因为该点是在单元格单击时转换。您的segue从控制器连接到详细视图控制器。在您的项目中 - 右键单击​​视图控制器,您将看到segue是手动连接的。但右键单击单元格不会显示segue连接。

    删除当前segue并重新创建它,这次是通过控制从单元格拖动到下一个视图控制器。然后,您可以通过右键单击单元格并确保segue已连接来仔细检查连接。

    应该看起来像:

    enter image description here

答案 1 :(得分:0)

  1. 如果segue应该从您的表转换到详细视图,并且如果您无法从表中查看详细信息视图,那么您的segue无效。

  2. 您可以使用SO编辑器顶部的代码示例按钮,也可以在文本编辑器中选择代码并在复制之前缩进代码。例如,在Xcode中,可以轻松选择代码,点击Command-],然后复制。然后粘贴到SO编辑器中。

  3. 请编辑您的问题以使其有意义。但是,我想你可能会问一些如何从你的表中推出视图控制器的问题。如果您正在使用segue,并且如果您已在故事板中创建了segue并且它已连接到源视图控制器和目标视图控制器,那么您可以将-performSegueWithIdentifier:sender:发送到您的表的视图控制器。也就是说,表视图控制器的-tableView:didSelectRowAtIndexPath:方法应该调用-performSegueWithIdentifier:sender:并指定从表视图控制器到详细视图控制器的segue的标识符。无论您是使用模拟器还是在真实设备上运行应用程序,都无关紧要。

答案 2 :(得分:0)

请参阅Caleb和Abizem关于1.和2的其他答案。如果我理解你的问题No 3,那么答案是否定的。当您想要选择表格行或详细信息封闭指示符时,您无需在模拟器中特别按某些内容。只需点击您在设备上点击的项目。如果它没有segue那么很可能它不是导致问题的模拟器:)

相关问题