我怎么能关闭UIView

时间:2012-05-10 09:33:28

标签: objective-c ios xcode uitableview uiview

我是iOS和Objective-C的新手。我有一个显示表视图的应用程序,并在单击用户单击某行时打开一个新视图。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {  
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    DetailViewController *detailController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
    [detailController changeSubjectText:[subject_data_Array objectAtIndex:indexPath.row]];

    //navigationController = [[UINavigationController alloc] initWithRootViewController:detailController];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible];
}

在我的详细视图中,我编码:

-(IBAction)closeDetail:(id)sender {
    NSLog(@"closeDetail");
    [self.view removeFromSuperview];
}

但它不起作用。有人可以帮忙吗?


任何人都可以帮助我吗?

我如何关闭视图?

下载我的代码 - > http://www.vasuta.com/ios/multiview2.zip

打开构建并运行单击“公告板”中的一行DetailView打开它单击关闭... ..

为什么DetailView不是全屏,为什么不能关闭详细视图?

我错了,或者我错了

请帮助我

你可以在“GadgetBulletinsTVContoller.m”中看到 didSelectRowAtIndexPath,你可以在“DetailViewController.m”中看到关闭命令

非常感谢

PS。对不起我的英语技能:(

2 个答案:

答案 0 :(得分:3)

为什么要创建该窗口对象?为什么要尝试将子视图添加到其中? 如果要添加子视图,则应将其添加到父视图,tableview视图或tableView的父视图中。

更好的想法是在堆栈上推送一个新的视图控制器,以显示您想要显示的信息。

这是一个教程,展示了如何在tableview tutorial link中选择单元格时推送新的视图控制器。

编辑: 在MultipleAppDelegate - (BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions应如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[MultipleViewController alloc] initWithNibName:@"MultipleViewController" bundle:nil];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    navController.navigationBarHidden = YES;
    self.window.rootViewController = navController;

    [self.window makeKeyAndVisible];
    return YES;
}

在GadgetBulletins中,TVContoller.h声明如下协议:

@protocol GadgetBulletinsTVControllerDelegate <NSObject>
@optional
- (void)showItemDetails:(id)selectedItem;

@end

和委托属性:

@property (nonatomic, assign)id<GadgetBulletinsTVControllerDelegate>delegate;

在GadgetBulletinsTVContoller.m中合成委托。 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 应该是这样的:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{   
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if([delegate respondsToSelector:@selector(showItemDetails:)])
    {
        [delegate showItemDetails:[subject_data_Array objectAtIndex:indexPath.row]];
    }
}

在FirstViewController.m中告诉控制器如下所示实现GadgetBulletinsTVControllerDelegate:

    @interface FirstViewController ()<GadgetBulletinsTVControllerDelegate>

in viewDidLoad method tell the gadgetBulletinsController that his delegate is the FirstViewController class, like this:

if (gadgetBulletinsContoller == nil) {
        gadgetBulletinsContoller = [[GadgetBulletinsTVContoller alloc] init];
        gadgetBulletinsContoller.delegate = self;
    }  

并实现GadgetBulletinsTVControllerDelegate的方法:

- (void)showItemDetails:(id)selectedItem
{
    if([delegate respondsToSelector:@selector(showDetailsScreenForItem:)])
    {
        [delegate showDetailsScreenForItem:selectedItem];
    }
}

在FirstViewController.h中声明如下协议:

@protocol FirstViewControllerDelegate <NSObject>

- (void)showDetailsScreenForItem:(id)item;

@end

并声明如下的委托属性(不要忘记在.m文件中合成):

@property (nonatomic, assign)IBOutlet id<FirstViewControllerDelegate>delegate;

在MultipleViewController.xib中选择FirstViewController屏幕并在outlet中从委托拖动到fileOwner,以将委托的值设置为MultipleViewController(如果需要,可以在代码中执行此操作)。

在MultipleViewController.m中告诉MultipleViewController如下所示实现FirstViewControllerDelegate协议:

@interface MultipleViewController ()<FirstViewControllerDelegate>

并实施协议方法:

- (void)showDetailsScreenForItem:(id)item
{
    DetailViewController *detailController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
    [detailController changeSubjectText:item];
    [self.navigationController pushViewController:detailController animated:YES];
}

在DetailViewController中修改closeDetail方法如下所示:

- (IBAction)closeDetail:(id)sender {
    NSLog(@"closeDetail");
    [self.navigationController popViewControllerAnimated:YES];
}

并且瞧,你的GadgetBulletinsTVController项目细节被推送。您需要为要显示详细信息的其他控制器执行相同的步骤。

答案 1 :(得分:-1)

而不是从窗口中删除视图只需使用self.window.rootviewcontroller重新加载包含所有表视图的基本视图

修改

我通过创建appDelegate获得了解决方案,您将要执行的操作如下所述

AppDelegate Here(MultipleAppDelegate)的第一个.h和.m文件

in .h add

@property (strong, nonatomic) id<UIApplicationDelegate>delegate;
<。>在.m add

@synthesize delegate;

现在,您想要添加detailView,只需在.h和.m文件中添加如下所述

在.h文件中(GadgetBulletinsTVContoller)

#import "MultipleAppDelegate.h"

并在接口一个变量中像这样

MultipleAppDelegate *Mydelegate;

viewDidLoadloadView方法

中的.m文件中
Mydelegate = [[UIApplication sharedApplication]delegate];

然后在加载detailView时执行此操作

navigationController = [[UINavigationController alloc] initWithRootViewController:detailController];
Mydelegate.window.rootViewController = navigationController;
[Mydelegate.window makeKeyAndVisible];

现在在detailViewController的.h和.m文件中 在.h文件中

#import "MultipleAppDelegate.h"

并在界面

MultipleAppDelegate *appDelegate;

viewDidLoadloadView方法

中的.m文件中
appDelegate = [[UIApplication sharedApplication]delegate];

并点击关闭按钮

//Not required
//[self.navigationController popViewControllerAnimated:NO];
appDelegate.viewController = [[MultipleViewController alloc] initWithNibName:@"MultipleViewController" bundle:nil];
appDelegate.window.rootViewController = appDelegate.viewController;
[appDelegate.window makeKeyAndVisible];

这就是它可以正常工作只有问题是它需要1或2秒来导航并显示multipalViewController

享受编码:) 快乐编码:)