【问题标题】:dimissModalViewControllerAnimated causing tableView to be full screen?dimissModalViewControllerAnimated 导致 tableView 全屏显示?
【发布时间】:2011-06-16 15:12:19
【问题描述】:

我有一个 RootViewController 有 2 个 tableViews 作为子视图(在 IB 中创建)每个都有自己的 tableViewController 类(处理 fetchRequests 等)

1 tableView 是静态的(用户或模型视图没有更改数据)。

tableView 2 在标题中有一个按钮,它显示了一个 imagePickerController。 到目前为止没有问题。

问题是,当我关闭 imagePicker 时

 [self dismissModalViewControllerAnimated:YES];

TableView 2 变成全屏我试过了

[[self rootViewController] dismissModalViewControllerAnimated:YES]

什么都没有发生。它粘在图像选择器上。

我怀疑这是因为以编程方式创建的视图很少。

有什么想法吗?

提前致谢。

去酒化

 -(IBAction)addImageTableAPressed {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];  
}

            RootViewController
             ||            ||
             ||            ||    
             \/            \/          addImageTableAPressed
  TableViewControlA  TableViewControlB --------------------->modelViewController

解决 managedObjectContect .....

     - (void)viewDidLoad {...
 if(managedObjectContext == nil) 
{ 
    managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
    NSLog(@"After managedObjectContext: %@",  managedObjectContext);
}
   ...
  }

【问题讨论】:

  • 如何呈现imagePickerController?你能发布一些代码吗?
  • 谢谢。哪个视图控制器实现了addImageTableAPressed 方法?如果presentModalViewController:animated: 的目标不是您的RootViewController,那么这可能会解释您的问题。另外,我建议不要使用不同的视图控制器管理根视图的部分或子视图...
  • @octy 欢呼,addImageTableAPressed 由 TableViewControllerB 实现(见上文)。我试图避免它,但唯一的方法是我可以在一个视图中获得 2 个 tableViews 并且能够为两者提供数据(获取请求)。我无法在单个类中创建超过 1 个提取请求。此外,将 rootViewCon 作为 ViewController,我可以拥有一个必不可少的 imageView。我知道父 viewCont 将是 tViewControllerB 并且当 imagePicker 被辞职时它被弹回到堆栈的顶部。我需要以某种方式使 rootView 成为父级,但保持设计不变..
  • 好的,在这种情况下,我会说最好定义一个包含 2 个子视图(TableViewA 和 TableViewB)的 UIView(rootView)。您的 RootViewController 的视图将是 rootView,并且此控制器必须是两个表视图的数据源和委托。我会写一个详细说明这种方法的答案,但首先,你为什么说你“不能在一个类中实现超过 1 个获取请求”?
  • 我的意思是,你有什么特别的限制来限制自己为每个类实现 1 个获取请求吗?

标签: iphone uitableview core-data uiviewcontroller


【解决方案1】:

正如我在我的一个 cmets 中提到的,我更喜欢使用单个视图控制器来管理两个表视图。定义一个 UIView(rootView),包括 2 个子视图(tableViewA 和 tableViewB)。您的 RootViewController 的视图将是 rootView,并且此控制器必须是两个表视图的数据源和委托。我将在此处提供的代码绝不是完整的,也不是最佳的,但可以让您很好地了解实现我的解决方案所需的内容。

例如:

@interface RootViewController <UITableViewDelegate, UITableViewDataSource> {
    NSArray *dataArrayA;
    NSArray *dataArrayB;
    UITableView tableViewA;
    UITableView tableViewB;
    NSManagedObjectContext *context;
}

@property (nonatomic, retain) NSArray *dataArrayA;
@property (nonatomic, retain) NSArray *dataArrayB;

// in IB, link the dataSource and delegate outlets of both tables to RootViewController
@property (nonatomic, retain) IBOutlet UITableView tableViewA;
@property (nonatomic, retain) IBOutlet UITableView tableViewB;

// this property will allow you to pass the MOC to the RootViewController from 
// the parent view controller, instead of accessing the app delegate from RootViewController
@property (nonatomic, retain) NSManagedObjectContext *context;

// ... etc.

@end



@implementation RootViewController

@synthesize dataArrayA;
@synthesize dataArrayB;
@synthesize tableViewA;
@synthesize tableViewB;
@synthesize context;

// initialize dataArrayA and dataArrayB
- (void)viewDidLoad {
    [super viewDidLoad];

    NSError *error = nil;

    // initialize and configure your fetch request for data going into tableViewA
    NSFetchRequest fetchRequestA = [[NSFetchRequest alloc] init];

    // configure the entity, sort descriptors, predicate, etc.
    // ...

    // perform the fetch
    self.dataArrayA = [context executeFetchRequest:fetchRequestA error:&error];

    // do the same for the data going into tableViewB - the code is very similar, you
    // could factor it out in a private method instead of duplicating it here
    // NSFetchRequest fetchRequestB = [[NSFetchRequest alloc] init]; 

    // omitting the details ... etc.

    self.dataArrayB = [context executeFetchRequest:fetchRequestB error:&error];

    // release objects you don't need anymore, according to memory management rules
    [fetchRequestA release];
    [fetchRequestB release];
}


// Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // if you have a different number of sections in tableViewA and tableViewB
/*
    if (tableView == tableViewA) {
        return ??;
    } else {
        return ??
    }
*/

    // otherwise, if both table views contain one section
    return 1;
}

// Customize the number of rows in each table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == tableViewA) {
        return [dataArrayA count];
    } else {
        return [dataArrayB count];
    }
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;

    if (tableView == tableViewA) {
        // get the data for the current row in tableViewA
        id objectA = [dataArrayA objectAtIndex:indexPath.row];

        // configure the cell for tableViewA
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierA];

        // etc...
    } else {
        // get the data for the current row in tableViewB
        id objectB = [dataArrayB objectAtIndex:indexPath.row];

        // configure the cell for tableViewB
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierB];
        // etc...
    }
    return cell;
}

// And so on, the same idea applies for the other UITableViewDelegate you would need to 
// implement...


- (void)dealloc {
    [dataArrayA release];
    [dataArrayB release];
    [tableViewA release];
    [tableViewB release];
    [context release];

    // etc...

    [super dealloc];
}

@end

我希望你会发现这很有用。

【讨论】:

  • 真正有用的信息好友!!!非常感谢您的努力和宝贵的课程,让您能够在单个类中执行多个获取请求。我养成了使用 - (NSFetchedResultsController *)fetchedResultsController 进行单次获取请求的习惯,因此限制了自己在一个类中拥有 2 个 tableView 的能力(我只需要在我以前的应用程序中使用 1 个 tableView )。代码现在已经使用您的提示进行了调整,并且工作起来就像一种享受。回头见!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多