Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 将按钮连接到xcode中的TableViewController_Iphone_Objective C_Uitableview_Xcode4.2 - Fatal编程技术网

Iphone 将按钮连接到xcode中的TableViewController

Iphone 将按钮连接到xcode中的TableViewController,iphone,objective-c,uitableview,xcode4.2,Iphone,Objective C,Uitableview,Xcode4.2,我的视图控制器中有一个按钮,我想将我的按钮连接到表视图控制器(这意味着当我单击按钮时,它会加载并带来我的表) 但我不知道该怎么做(我可以将我的按钮连接到另一个视图控制器,但不能连接到表),这是我的代码 提前谢谢!(我真的是初学者) FirstViewController.h #import <UIKit/UIKit.h> @interface FirstViewController : UIViewController @end CreateViewController.h//

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

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

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

FirstViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@end
CreateViewController.h//my table

 #import <UIKit/UIKit.h>

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

 }

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

编辑:我使用了故事板

不要将UITableView的代码放在viewDidLoad()方法中

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


如果你找不到,请问我……我很乐意帮助你

我想按钮在FirstViewController中。如果是,请执行-(iAction)单击按钮并编写代码,然后将其连接到Interface Builder的底部(如果使用Interface Builder)。在FirstViewController.h中写入
createViewController
对象和
#导入

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@end
在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

在第一个视图中,创建一个名为btn1的buton

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

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

现在在加载时使表隐藏

写入[tablename sethidden:是]

现在,在button的操作方法上,[tablename sethdden:NO]

tableData=[[NSArray alloc]initWithObjects:@“Johan”,“Paul”,“George”,“Ringo”,nil]

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

必须采用三种方法:

  • -(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节

  • -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

  • -(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath


  • 试试这个……如果有什么查询发生,请告诉我

    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
    
    #导入
    @界面FirstViewController:UIViewController
    {
    UITableView*maintableView;
    NSArray*表格数据;
    }  
    @属性(非原子,保留)IBUITableView*maintableView;
    -(i)单击;
    @结束
    FirstViewController.m
    #导入“FirstViewController.h”
    @FirstViewController的实现
    @综合主表视图;
    -(无效)viewDidLoad
    {
    [maintableView setHidden:是];
    [超级视图下载];
    }
    -(无效)视图卸载
    {
    [超级视频下载];
    }
    -(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation
    {
    if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone){
    返回(interfaceOrientation!=UIInterfaceOrientation肖像向上向下);
    } 
    其他的
    {
    返回YES;
    }
    -(iAction)单击
    {
    [maintableView设置隐藏:否];
    tableData=[[NSArray alloc]initWithObjects:@“Johan”,“Paul”,“George”,“Ringo”,nil];
    }
    -(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
    {
    返回[表数据计数];
    }
    -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
    {
    UITableViewCell*单元格=nil;
    单元格=[tableView dequeueReusableCellWithIdentifier:@“MyCell”];
    如果(单元格==nil)
    {
    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:@“MyCell”];
    }
    cell.textlab.text=[tableData objectAtIndex:indexath.row];
    返回单元;
    }
    }
    @结束
    
    正如您所说的故事板:

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

    之后

    • 拖放您的表视图
    • 将按钮连接到带有模块的表视图 -单击表格并从菜单中选择-->编辑器-->嵌入-->并添加导航控制器
    使用此过程,您可以轻松地进行GUI演示,而无需编写代码,然后您应该创建TableViewController文件并添加代码

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


    希望这有帮助

    非常感谢,但是我应该把我的代码放在UITableView的哪里呢?我是初学者,我不知道应该放在哪里。@justin:正如你所说,“我可以将我的按钮连接到另一个视图控制器,但不能连接到表”;然后,在单击按钮时,放置该操作方法的一些代码。在该方法中,您可以将代码行写入UITableView的pushViewController。如果你想点击按钮,它应该加载并带来你的表;那么,您的表视图代码与她无关,但您在单击按钮时执行操作的代码很重要。编辑你的答案accordingly@justin:“Prasad G”的答案可以帮助您获得解决方案。@hpiOSCoder但它还不起作用我现在有一个错误:这行有一个错误—(iAction)clickButton(id)sender;{我的错误是:在方法之后应为“;”prototype@justin(iAction)单击按钮:(id)发件人;编写此内容无需创建两个视图…如果需要创建两个视图而不是单击按钮,请通过NSDefaultUser方法移动到第二个视图。谢谢,但如果您检查我的代码,我已经编写了这3种方法,但我不知道为什么要将其设置为隐藏或不隐藏,您是否可以更改我的代码并编写您的确切意思我已经定了赛斯
    @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;
    }
    
        #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