如何在iOS中解除它之后呈现相同的视图控制器

时间:2014-05-09 22:00:42

标签: ios objective-c

在我的应用中,我有一个UITabBarController,它有四个视图控制器。在每个视图控制器中,我都有一个集合视图。在每个集合视图中,我都有一些图像。当我从第三个视图控制器中选择一个图像时,它会打开一个不在UITabBarController中的Web视图控制器。

在网页视图控制器中,我在后面有一个带导航栏的后退按钮。按下后退按钮后,它将返回第三个视图控制器。再次当我在第三个视图控制器中选择另一个图像时,它应该打开Web视图控制器,但是Web视图控制器没有出现在模拟器上而是说错误1thread,1breakpoint。

这是我的代码:

选择图像后,此代码位于thirdviewcontroller中

webViewController = [[AppsWebViewController alloc]init];
[self presentViewController:webViewController animated:YES completion:nil];

按下后退按钮后,此代码位于webviewcontroller中

[self dismissViewControllerAnimated:YES completion:nil];

我需要在四个视图控制器中的任何一个中选择任何图像后每次打开webviewcontroller。

我的第三个视图控制器代码:

#import "FreqAppsThirdViewController.h"


@interface FreqAppsThirdViewController ()

@end

@implementation FreqAppsThirdViewController

- (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.

icons = [NSArray arrayWithObjects:@"amazon-1.png",@"best_buy.png",@"Carl-Icahn-Lectures-Apple-Gambles-Netflix-and-Threatens-eBay-2.jpg",@"index.jpg",@"Office-Max.jpg", nil];
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
_collectionView=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
[_collectionView setDataSource:self];
[_collectionView setDelegate:self];

[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[_collectionView setBackgroundColor:[UIColor whiteColor]];

[self.view addSubview:_collectionView];

[super viewDidLoad];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return icons.count;
}

// The cell that is returned must be retrieved from a call to -  dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:[icons objectAtIndex:indexPath.row]]];
return cell;
}

-(void)collectionView:(UICollectionView *)collectionViewdidSelectItemAtIndexPath:(NSIndexPath *)indexPath {

 if (indexPath.row == 0) {


    NSURL *url = [NSURL URLWithString:@"http://www.amazon.com/"];


    webViewController = [[FreqAppsWebViewController alloc]initWithURL:url andTitle:@"Amazon"];
    [self presentViewController:webViewController animated:YES completion:nil];

  } else if (indexPath.row == 1){
        NSURL *url = [NSURL URLWithString:@"http://www.bestbuy.com/"];


    webViewController = [[FreqAppsWebViewController alloc]initWithURL:url andTitle:@"Best Buy"];
    [self presentViewController:webViewController animated:YES completion:nil];
  } else if (indexPath.row == 2){
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.ebay.com"]];
  } else if (indexPath.row == 3){
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.walmart.com"]];
  } else if (indexPath.row == 4){
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.officemax.com"]];
  }
  // datasetCell.backgroundColor = [UIColor blueColor]; // highlight selection
  }

  - (CGSize)collectionView:(UICollectionView *)collectionView layout (UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  {
   return CGSizeMake(50, 50);
  }

  - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout(UICollectionViewLayout*)collectionViewLayoutinsetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(50, 20, 50, 20);
  }

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

@end

我的webviewcontroller代码:

#import "FreqAppsWebViewController.h"
#import "FreqAppsThirdViewController.h" 
#import "FreqAppsAppDelegate.h"

@interface FreqAppsWebViewController ()

@end

@implementation FreqAppsWebViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {

}
return self;
}



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

- (id)initWithURL:(NSURL *)url andTitle:(NSString *)string {
if( self = [super init] ) {
    theURL = url;
    theTitle = string;
}
return self;
}

-(id)initWithURL:(NSURL *)url {
 return [self initWithURL:url andTitle:nil];
}

- (void)viewDidLoad {
[super viewDidLoad];
webTitle.title = theTitle;
NSURLRequest *requestObject = [NSURLRequest requestWithURL:theURL];
[webView loadRequest:requestObject];
}

- (IBAction) back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
webView.delegate = nil;
[webView stopLoading];
}

@end

我的webviewcontroller.h代码

#import <UIKit/UIKit.h>

@interface FreqAppsWebViewController : UIViewController <UIWebViewDelegate>
{
NSURL *theURL;
NSString *theTitle;
IBOutlet UIWebView *webView;
IBOutlet UINavigationItem *webTitle;

}

- (id)initWithURL:(NSURL *)url;
- (id)initWithURL:(NSURL *)url andTitle:(NSString *)string;
- (IBAction)back:(id)sender;
@end

我的thirdviewcontroller.h代码:

#import <UIKit/UIKit.h>
#import "FreqAppsWebViewController.h"


@interface FreqAppsThirdViewController:UIViewController<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
UICollectionView *_collectionView;
NSArray *icons;
FreqAppsWebViewController *webViewController;

}

@end

1 个答案:

答案 0 :(得分:1)

复制并粘贴您收到的完整错误消息。

如果它是一个断点,而不是一个错误,就像你似乎指出的那样,可能只是你点击了你的源边缘并设置了断点而没有意识到它。

检查断点:

按Command 7显示断点导航器,查看代码中是否设置了断点。如果有,您将以目标(当前应用程序)开始,然后是源文件.m,然后是列出方法名称和行号的条目,其中的符号看起来像是指向右侧之间的交叉箭头和蓝色粘滞便笺。

如果有断点,请一次选择一个断点并记下它们在代码中的位置。那么你的IBAction方法,还是你的AppsWebViewController中的方法之一?

通过选择它并按删除键删除每个断点。然后再次运行你的程序。