我有一个父视图控制器,它包含以编程方式创建的两个视图和另一个在其中一个视图中单击链接时动态创建的视图控制器。
在这个动态创建的视图控制器中,我有一个后退按钮也有一个处理程序,当单击后退按钮时,自定义视图控制器将退出视图,然后将调用父视图控制器中的方法,但逻辑在调用的方法不起作用。
点击链接时添加的自定义视图控制器:
#import <UIKit/UIKit.h>
#import "ViewController.h"
@interface UIListingViewController : UIViewController <UIGestureRecognizerDelegate,UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegat e>
@property (nonatomic, strong) UITableView *uiTableView1;
@property (nonatomic, strong) UIButton *uiButton;
@property (nonatomic, strong) ViewController *uiVC;
@property (nonatomic, strong) UINavigationBar *uiNavigationBar;
@property (nonatomic, strong) UIView *uiView;
@end
#import "UIListingViewController.h"
#import "Listing.h"
@interface UIListingViewController ()
@end
@implementation UIListingViewController
@synthesize uiButton;
@synthesize uiTableView1;
@synthesize uiVC;
@synthesize uiNavigationBar;
@synthesize uiView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
uiView = [[UIView alloc] init];
[uiView setFrame:CGRectMake(0, 0, 320, 480)];
[uiView setBackgroundColor:[UIColor grayColor]];
[self.view addSubview:uiView];
//self.view = uiView;
uiNavigationBar = [[UINavigationBar alloc] init];
[uiNavigationBar setFrame:CGRectMake(0, 0, 320, 50)];
[uiView addSubview:uiNavigationBar];
//uiButton.layer.zPosition = 15;
uiButton = [[UIButton alloc] init];
[uiButton setTitle:@"Back" forState:UIControlStateNormal];
[uiButton setFrame:CGRectMake(0, 20, 80, 25)];
[uiButton addTarget:self action:@selector(myButtonClick) forControlEvents:UIControlEventTouchUpInside];
//uiButton.layer.zPosition = 15;
[uiNavigationBar addSubview:uiButton];
uiTableView1 = [[UITableView alloc] init];
[uiTableView1 setFrame:CGRectMake(0, 50, 320, 480)];
uiTableView1.dataSource = self;
uiTableView1.delegate = self;
uiTableView1.scrollEnabled = YES; //uiTableView1.layer.zPosition = 15;
[uiView addSubview:uiTableView1];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)myButtonClick {
// button was tapped - do something
[UIView animateWithDuration:0.7f
animations:^
{
CGRect frame = self.view.frame;
frame.origin.x = 0;
frame.origin.y = 480;
self.view.frame = frame;
}
completion:^(BOOL finished)
{
[self.uiVC backToViewStart];
}
];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"newFriendCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newFriendCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
Listing *listing = (Listing *) [self.uiVC.data objectAtIndex:indexPath.row];
cell.textLabel.text = listing.name;
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.uiVC.data count];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
这是在父视图控制器的其中一个视图中点击链接时创建自定义视图控制器的点击手势:
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
CGPoint location = [recognizer locationInView:[recognizer.view superview]];
CGPoint touchPoint=[recognizer locationInView:[recognizer.view superview]];
UIViewMenuItem *tempView = (UIViewMenuItem *)recognizer.view;
NSNumber *tag = [NSNumber numberWithInt:tempView.tag];
NSString *idCat = [tempView getCatId];
NSLog(@"TAG %@",idCat);
//NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: [NSString stringWithFormat:@"http://localhost:8888/MAMP/WHFC/SubCategories.php?categoryid//=%d", idCat]]];
NSString *myRequestString = [NSString stringWithFormat:@"categoryid=%@",idCat];
// Create Data from request
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://localhost:8888/MAMP/WHFC/SubCategories.php"]];
// set Request Type
[request setHTTPMethod: @"POST"];
// Set content-type
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody: myRequestData];
// Now send a request and get Response
NSData *data = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
// Log Response
NSString *response = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
NSError *err;
NSArray *arrCategoryList = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&err];
const unsigned char *ptr = [data bytes];
for(int i=0; i<[data length]; ++i) {
unsigned char c = *ptr++;
NSLog(@"char=%c hex=%x", c, c);
}
self.data = [[NSMutableArray alloc] init];
for(NSDictionary *dictCategory in arrCategoryList)
{
NSString *strCategoryId = [dictCategory objectForKey:@"CategoryId"];
NSString *strCategoryName = [dictCategory objectForKey:@"Name"];
NSLog(@"%@ : %@",strCategoryId,strCategoryName);
Listing *listing = [[Listing alloc] init];
listing.catId = strCategoryId;
listing.name = strCategoryName;
[self.data addObject:listing];
}
//UIViewController *viewController = [[UIViewController alloc] init];
//viewController.view.backgroundColor = [UIColor whiteColor];
UIListingViewController *viewController = [[UIListingViewController alloc] init];
viewController.view.backgroundColor = [UIColor whiteColor];
viewController.uiVC = self;
[self presentViewController:viewController animated:YES completion:nil];
[UIView animateWithDuration:0.7f
animations:^
{
CGRect frame = viewSlide3.frame;
frame.origin.x = 0;
frame.origin.y = -480;
viewSlide3.frame = frame;
}
completion:^(BOOL finished)
{
}
];
//Do stuff here...
}
这是我想从自定义视图控制器在父视图控制器中调用的方法:
- (void) backToViewStart{
CGRect frame = viewSlide3.frame;
frame.origin.x = 10;
frame.origin.y = 0;
viewSlide3.frame = frame;
CGRect frame2 = viewSlide.frame;
frame2.origin.x = 0;
frame2.origin.y = 0;
viewSlide.frame = frame2;
}
但逻辑永远无效我只是得到一个空白屏幕。为什么呢?
答案 0 :(得分:1)
我可以看到你实际上将ListViewController的视图设置为底层,但实际上你从未解除过UIListingViewController。
尝试 在ListViewController上
- (void)myButtonClick
{
[self.uiVC backToViewStart];
}
在ParentViewController上
- (void) backToViewStart
{
[self dismissViewControllerAnimated:YES completion:nil];
}
让我知道它是否有效。