0

我有以下代码:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {



    UIImage * search = [UIImage imageNamed: @"search.png"];
    UIImageView * s = [[UIImageView alloc] initWithImage: search];
    s.frame = CGRectMake(15, 14, search.size.width, search.size.height);

    UIImage * simg = [UIImage imageNamed: @"m_search_back.png"];
    UIImage * img = [UIImage imageNamed: @"m_top.png"];
    CGRect searchBarFrame = CGRectMake(0, 0, self.tableView.frame.size.width, 45.0);
    self.searchBar = [[UISearchBar alloc] initWithFrame:searchBarFrame];
    self.searchBar.backgroundImage = simg;
    [self.searchBar setSearchFieldBackgroundImage: simg forState:UIControlStateNormal];
    self.searchBar.delegate = self;
    [self.searchBar addSubview: s];


    UITextField *searchField = [self.searchBar valueForKey:@"_searchField"];

    // Change search bar text color
    searchField.textColor = [UIColor colorWithRed: 178.0/255 green:178/255.0 blue:178/255.0 alpha:1.0];
    searchField.font = [UIFont fontWithName: @"HelveticaNeue-Light" size: 16.0f];

    // Change the search bar placeholder text color
    [searchField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    [self.searchBar setPlaceholder:@"Search for investors, startups, founders, etc"];


    UIImageView * top = [[UIImageView alloc] initWithImage: img];
    top.frame = CGRectMake(0, self.searchBar.frame.size.height + 1, 800, img.size.height);

    UILabel * topl = [[UILabel alloc] initWithFrame: CGRectMake(30, 10, 800, img.size.height - 30)];
    topl.text = @"Connect with\nFounders and Investors";
    topl.numberOfLines = 0;
    topl.backgroundColor = [UIColor clearColor];
    topl.textColor = [UIColor whiteColor];
    topl.textAlignment = NSTextAlignmentCenter;
    [topl setFont: [UIFont fontWithName: @"HelveticaNeue-Bold" size: 17.0f]];
    [topl sizeToFit];
    [top addSubview: topl];

    UIImage * loginbtn = [UIImage imageNamed: @"loginbtn.png"];
    UIImage * sep1 = [UIImage imageNamed: @"m_sep1.png"];


    UIButton * login = [[UIButton alloc] initWithFrame: CGRectMake(30, 70, loginbtn.size.width, loginbtn.size.height)];
    [login.titleLabel setFont: [UIFont fontWithName: @"HelveticaNeue-Medium" size: 16.0f] ];
    [login setTitle:@"Login" forState: UIControlStateNormal];
    [login addTarget: self action:@selector(loginUser:) forControlEvents:UIControlEventTouchUpInside];
    [login setBackgroundImage: loginbtn forState: UIControlStateNormal];
    [top addSubview: login];


    UIImage * greenbtn = [UIImage imageNamed: @"greenbtn.png"];
    UIButton *  green = [[UIButton alloc] initWithFrame: CGRectMake(loginbtn.size.width + 50, 70, greenbtn.size.width, greenbtn.size.height)];
    [green setTitle:@"Register" forState: UIControlStateNormal];
    [green.titleLabel setFont: [UIFont fontWithName: @"HelveticaNeue-Medium" size: 16.0f] ];
    [green  setBackgroundImage: greenbtn forState: UIControlStateNormal];
    [green addTarget:self action:@selector(registerUser:) forControlEvents:UIControlEventTouchUpInside];
    [top addSubview: green];

    UIImageView * sep2 = [[UIImageView alloc] initWithImage: sep1];
    sep2.frame = CGRectMake(0, top.frame.origin.y + top.frame.size.height - 2, top.frame.size.width, 1);


    UIImage * sep5img = [UIImage imageNamed: @"m_sep5.png"];
    UIImageView * sep5 = [[UIImageView alloc] initWithImage: sep5img];
    sep5.frame = CGRectMake(0, self.searchBar.frame.origin.y + self.searchBar.frame.size.height, self.searchBar.frame.size.width, sep5img.size.height);

    UIView * toph = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 800, simg.size.height + top.frame.size.height + 2)];


    [toph addSubview: self.searchBar];
    [toph addSubview: sep5];
    [toph addSubview: top];
    [toph addSubview: sep2];

    return toph;
}

不幸的是,我尝试向 UIButton“登录”添加一个操作,但它不起作用。未调用 loginUser 函数。我能做些什么?我是否正确设置了 UIButton 上的操作?

谢谢。

4

1 回答 1

5

我相信 imageViews 默认将 userInteractionEnabled 设置为 NO。尝试添加:

top.userInteractionEnabled = YES;
于 2013-05-09T11:57:44.427 回答