【问题标题】:connect button to TableViewController in xcode将按钮连接到 xcode 中的 TableViewController
【发布时间】:2012-05-05 00:15:30
【问题描述】:

我的视图控制器中有一个按钮,我想将我的按钮连接到表格视图控制器(这意味着当我点击按钮时,它会加载并带来我的表格)

但我不知道该怎么做(我可以将我的按钮连接到另一个视图控制器但不能连接到表格),这是我的代码

提前谢谢!(我真的是初学者)

FirstViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@end

FirstViewController.m

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
    return YES;
}
}

@end

CreateViewController.h //我的表

 #import <UIKit/UIKit.h>

 @interface CreateViewController : UIViewController <
 UITableViewDataSource, UITableViewDelegate>
 {
 NSArray *tableData;

 }

 @property (nonatomic, retain) NSArray *tableData;
 @end

CreateViewController.m

#import "CreateViewController.h"

@implementation CreateViewController
@synthesize tableData;


#pragma mark - View lifecycle

- (void)viewDidLoad
{
tableData = [[NSArray alloc] initWithObjects:@"Johan", @"Paul",@"George",@"Ringo", nil];
[super viewDidLoad];
// Do any additional setup after loading the view.
}

#pragma mark - TableView Data Source methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];

}


 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;

cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
if(cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"];

}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];

return cell;
}


 @end

编辑:我使用了故事板

【问题讨论】:

  • @Marc W 你能帮帮我吗?

标签: iphone objective-c uitableview xcode4.2


【解决方案1】:

我认为按钮在 FirstViewController 中。如果是,则执行 -(IBAction)clickButton 并编写代码并将其连接到 Interface Builder 的底部(如果您使用 Interface Builder)。在FirstViewController.h中写入createViewController对象和#import &lt;CreateViewController.h&gt;

在 FirstViewController.h 中,

#import "CreateViewController.h"

@interface FirstViewController : UIViewController{

    CreateViewController *createViewController;
}
-(IBAction)clickButton:(id)sender;
@end

在 FirstViewController.m 中,你只需添加下面的方法

 -(IBAction)clickButton:(id)sender{

if (!createViewController) {
                createViewController = [[CreateViewController alloc] initWithNibName:@"CreateViewController" bundle:nil];

            }

            UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
            self.navigationItem.backBarButtonItem = backBarButtonItem;
            [backBarButtonItem release];
            [self.navigationController pushViewController:createViewController animated:YES];
}

在 AppDelegate.h 中,

#import "FirstViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) FirstViewController *viewController;
@property (nonatomic, retain) UINavigationController *navControl;
@end

在 AppDelegate.m 中,

@synthesize window;
@synthesize viewController;
@synthesize navControl;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
    navControl = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window addSubview:[navControl view]];
    [self.window makeKeyAndVisible];
    return YES;
}

【讨论】:

  • 谢谢,但是,我有这个错误:type name required specifier or qualifier xcode is there and id()-->somthing for this one -(IBOutlet)clickButton?
  • 哦!对不起。 Sharjeel 编辑了我的代码并检查了它。如果您有任何错误,请写 -(IBAction)clickButton:(id)sender 代替它。
  • 但我在这一行仍然有一个错误 -(IBAction)clickButton(id)sender;{ 我的错误是:预期的“;”方法原型之后
  • -(IBAction)clickButton:(id)sender ---- 在 clickButton 和 (id) 之间有一个分号 (:)。请放上去。
  • 这里仍然有错误:FirstViewController.m 类中此行的预期表达式错误 -(IBAction)click { - 我使用情节提要只有一条评论,我是否应该通过 (push,model) 将按钮连接到导航控制器或 ....) 还是我应该将按钮连接到 tableview 控制器?
【解决方案2】:

正如你所说的故事板:

首先浏览 iphone 故事板并创建您的视图,然后添加您的按钮

之后

  • 拖放您的表格视图
  • 使用模块将您的按钮连接到表格视图 -单击表格并从菜单中选择-->编辑器-->嵌入-->并添加导航控制器

通过这个过程,您无需编写代码即可轻松拥有您的 GUI 演示,然后您应该创建 TableViewController 文件并添加您的代码,

只是不要忘记 --> 当您完成 GUI 部分和代码后,您应该通过自定义类中的对象将 createViewController 类添加到您的 GUI。

希望这会有所帮助!

【讨论】:

    【解决方案3】:

    FirstViewController.h

        #import <UIKit/UIKit.h>
    
        @interface FirstViewController : UIViewController
       <UITableViewDataSource,UITableViewDelegate>
              {
                 UITableView *maintableView;
                 NSArray *tableData;
    
    
              }  
    
            @property (nonatomic,retain)IBOutlet UITableView *maintableView;
              -(IBAction)click;
    
           @end
    
              FirstViewController.m
    
                  #import "FirstViewController.h"
    
                  @implementation FirstViewController
                  @synthesise maintableView;
                    - (void)viewDidLoad
                        {
    
                        [maintableView setHidden : YES];
                        [super viewDidLoad];
    
                       }
    
                   - (void)viewDidUnload
                         {
                           [super viewDidUnload];
    
                         }
    
             - (BOOL)shouldAutorotateToInterfaceOrientation:     (UIInterfaceOrientation)interfaceOrientation
                      {
                if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
                         return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
                          } 
         else 
         {
              return YES;
              }
      -(IBAction)click
         {
              [maintableView setHidden : NO];
           tableData = [[NSArray alloc] initWithObjects:@"Johan", @"Paul",@"George",@"Ringo", nil];
           }
    
        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
          {
           return [tableData count];
    
           }
    
    
           - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
            {
                UITableViewCell *cell = nil;
    
               cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
            if(cell == nil)
              {
             cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"];
    
             }
           cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
    
           return cell;
       }
    
        }
        @end
    

    【讨论】:

    • Give outlets....MaintableView 应该连接到 tableview....连接数据源和委托方法...单击 () 将连接到按钮.....希望你能得到你的解决方案.....
    • 在.h文件中声明方法后:-(IBAction)click;这里 ;是必须的.​​..可能是您缺少分号...
    • 现在我在 FirstViewController.m 类中出现预期表达式错误 -(IBAction)click {
    • 这里仍然有错误:FirstViewController.m 类中此行的预期表达式错误 -(IBAction)click { - 我使用情节提要只有一条评论,我是否应该通过 (push,model) 将按钮连接到导航控制器或 ....) 还是我应该将按钮连接到 tableview 控制器?
    • @ughoavgfhw 你能帮帮我吗?
    【解决方案4】:

    在第一个视图中,创建一个名为 btn1 的按钮。

    将它设置在模拟器屏幕的顶部。

    现在从对象库中获取 tableview。把它放在按钮下面.....

    现在在加载时隐藏表格....

    写[tablename sethidden: YES]

    现在,关于按钮的操作方法,[tablename sethdden : NO]

    tableData = [[NSArray alloc] initWithObjects:@"Johan", @"Paul",@"George",@"Ringo", nil];

    然后写出table所需的所有方法:

    三个方法是必须的:

    1. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    2. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath

    3. -(void)tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    试试这个....如果有问题请告诉我

    【讨论】:

    • 无需创建两个视图......如果需要创建两个视图而不是单击按钮,则通过 NSDefaultUser 方法移动到第二个视图。
    • 谢谢,但是如果您检查我的代码,我已经编写了这 3 种方法,但我不知道为什么应该隐藏它,您能否更改我的代码并写下您的意思?
    • 我已将其设置为 setHidden,因为您想在 button_click 上显示 tableView....知道吗?
    【解决方案5】:

    不要把你的 UITableView 代码放在 viewDidLoad() 方法中..

    创建一个按钮并在加载时显示它...在按钮的操作方法上显示表格并加载数据...

    如果你不懂就问我....我很乐意帮助你

    【讨论】:

    • 非常感谢,但是我应该把我的代码放在 UITableView 哪里,我是初学者,我不知道我应该把它放在哪里。
    • @justin:正如你所说“我可以将我的按钮连接到另一个视图控制器,但不能连接到表格”;然后放置一些在单击按钮时执行的操作方法的代码。在该方法中,您可以编写一行代码到 UITableView 的 pushViewController。如果你想要点击按钮,它应该加载并带来你的桌子;那么您的表格视图代码与她无关,但您在单击按钮时执行操作的代码很重要。相应地编辑你的答案
    • @justin:“Prasad G”的回答可以帮助您获得解决方案。
    • @hpiOSCoder 但它不起作用我现在有这个错误:这一行中的错误 -(IBAction)clickButton(id)sender;{ 我的错误是:预期的“;”方法原型之后
    • @justin (IBAction)clickButton:(id)sender;写这个
    猜你喜欢
    • 1970-01-01
    • 2012-12-06
    • 2017-08-20
    • 2014-05-10
    • 1970-01-01
    • 2021-12-16
    • 2014-09-18
    • 2013-02-19
    • 1970-01-01
    相关资源
    最近更新 更多