ios - 方法确实在父 View Controller 中调用,但方法内部的逻辑从不执行任何操作

标签 ios objective-c

我有一个父 View Controller ,其中包含两个以编程方式创建的 View 和另一个在其中一个 View 中单击链接时动态创建的 View Controller 。

在这个动态创建的 View Controller 中,我有一个后退按钮,它也有一个处理程序,当单击后退按钮时,自定义 View Controller 将退出 View ,然后将调用父 View Controller 中的方法,但逻辑调用的方法不起作用。

点击链接时添加的自定义 View Controller :

#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.
}

这是在父 View Controller 的其中一个 View 中点击链接时创建自定义 View Controller 的点击手势:

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

这是我想从自定义 View Controller 在父 View Controller 中调用的方法:

- (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;
}

但是这个逻辑永远不起作用,我只是得到一个空白屏幕。为什么?

最佳答案

我可以看到您实际上将 ListingViewController 的 View 动画到了底部,但实际上您从未关闭 UIListingViewController。

尝试 在 ListViewController 上

- (void)myButtonClick 
{
     [self.uiVC backToViewStart];
}

在父 View Controller 上

- (void) backToViewStart
{
     [self dismissViewControllerAnimated:YES completion:nil];
}

让我知道它是否有效。

关于ios - 方法确实在父 View Controller 中调用,但方法内部的逻辑从不执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23295103/

相关文章:

iphone - 挂接到 UIPopoverDelegate 方法以调整弹出窗口的大小 : contentSizeForViewInPopover

objective-c - *准确*计算 Cocoa 中的文本高度(适用于 Mac,不适用于 iOS)

iphone - 我使用 SD 图像构建我的应用程序,如何支持 HD?

javascript - 视频.js播放器在手机上单击即可暂停/播放

iphone - calloutAccessoryControlTapped 未在 Storyboard中调用

ios - stringWithContentsOfURL返回(空)

ios - 如果点击 UIButton,如何显示搜索栏

ios - Objective-C:获取每个表行的文本字段值

ios - 在 Swift 中调用 SignalR 方法 (SwiftR)

ios - 重写具有特殊类型(类)作为参数的函数?