2013-05-09 69 views
0

我有以下代码:UIButton的行动选择

- (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的动作?

谢谢。

+0

你在哪里定义'loginUser'方法?显示此代码 – Mani 2013-05-09 11:52:44

+0

如何编写您的登录用户功能 – 2013-05-09 11:52:48

+1

top.userInteractionEnabled = YES; – Buntylm 2013-05-09 11:54:42

回答

5

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

top.userInteractionEnabled = YES;