Ios 按钮不点火

Ios 按钮不点火,ios,objective-c,Ios,Objective C,我在UIApgestureRecognitor中动态创建了一个UIButton,我设置了按钮处理程序,但当按下按钮时,该处理程序从未被调用 以下是我如何在点击手势中创建UIButton: UIButton *uiButton = [[UIButton alloc] init]; [uiButton setTitle:@"Back" forState:UIControlStateNormal]; [uiButton setFrame:CGRectMake(0, 20, 80, 25)]; [ui

我在UIApgestureRecognitor中动态创建了一个UIButton,我设置了按钮处理程序,但当按下按钮时,该处理程序从未被调用

以下是我如何在点击手势中创建UIButton:

 UIButton *uiButton = [[UIButton alloc] init];
[uiButton setTitle:@"Back" forState:UIControlStateNormal];
[uiButton setFrame:CGRectMake(0, 20, 80, 25)];
[uiButton addTarget:self action:@selector(myButtonClick) forControlEvents:UIControlEventTouchDown];
这是按钮处理程序:

- (void)myButtonClick {
// button was tapped - do something
if(NULL) {

}

}
这已被编辑:

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

UIView *uiView = [[UIView alloc] init];
[uiView setFrame:CGRectMake(0, 480, 320, 480)];
[uiView setBackgroundColor:[UIColor grayColor]];

viewController.view = uiView;

UINavigationBar *uiNavigationBar = [[UINavigationBar alloc] init];
[uiView setFrame:CGRectMake(0, 0, 320, 50)];
[uiView addSubview:uiNavigationBar];

UIButton *uiButton = [[UIButton alloc] init];
[uiButton setTitle:@"Back" forState:UIControlStateNormal];
[uiButton setFrame:CGRectMake(0, 20, 80, 25)];
[uiButton addTarget:self action:@selector(myButtonClick) forControlEvents:UIControlEventTouchDown];

[uiNavigationBar addSubview:uiButton];

uiTableView1 = [[UITableView alloc] init];
[uiTableView1 setFrame:CGRectMake(0, 50, 320, 480)];
uiTableView1.dataSource = self;
uiTableView1.delegate = self;

[uiView addSubview:uiTableView1];

[self presentViewController:viewController animated:YES completion:nil];



//Do stuff here...
}

- (void)myButtonClick {
// button was tapped - do something
if(NULL) {

}

}

也许你的手势识别器捕捉到了触摸,因此永远也够不到按钮。要让按钮获取事件,您必须设置UIgestureRecognitizerDelegate的委托,并实现如下内容

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ((touch.view == mybutton)||(touch.view == anyOtherButton)) {
        return NO;
    }
    return YES;
}
希望它有帮助

您犯的最大错误是用于控制事件的uicontrol事件触地。你的要求在这里不同`

解决方案:当按钮被点击时,它应该是UIControlEventTouchUpInside

用你的替换下面的行

[uiButton addTarget:self action:@selector(myButtonClick) forControlEvents:UIControlEventTouchUpInside];
[uiView setFrame:CGRectMake(0, 0, 320, 480)];
编辑:按钮的superview也超出了范围,即y是480,它应该是0

用你的替换下面的行

[uiButton addTarget:self action:@selector(myButtonClick) forControlEvents:UIControlEventTouchUpInside];
[uiView setFrame:CGRectMake(0, 0, 320, 480)];

在何处将其添加到视图?如果为空,其目的是什么?1按钮是否显示?我想是的2您没有忘记将按钮添加到父视图,对吗?我想没有,你没有忘记3,如果空值怎么办?该条件中的代码永远不会执行。ifNULL只是在那里放置一个断点,以便我知道它将进入方法按钮放置在另一个视图中,而不是父视图您是否尝试使用+idbuttonWithType:UIButtonTypebuttonType创建它?谢谢,但不起作用,可能是因为在包含按钮的视图下有另一个视图吗?@redoc01现在检查它是否找到了您的解决方案。我将480设置为0,但仍然没有运气