connect button to TableViewController in xcode

主宰稳场 提交于 2019-11-28 02:20:57

I think the button is in FirstViewController. If it is then implement -(IBAction)clickButton and write code and connect it to your bottom in Interface Builder(If you use Interface Builder) . write createViewController object and #import <CreateViewController.h> in FirstViewController.h

In FirstViewController.h,

#import "CreateViewController.h"

@interface FirstViewController : UIViewController{

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

In FirstViewController.m, you just add below method

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

and in 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

In 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;
}

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

Don't put your code of UITableView in viewDidLoad () method..

create one button and show it on load time...On button's action method show the table and load the data...

If you can't get tha ask me....I will love to help you

In the first view ,Create one buton named btn1.

set it on the top of simulator screen.

Now take tableview from object library. put it under the button.....

Now in load time make the table hidden....

Write [tablename sethidden: YES]

Now, on button's action method , [tablename sethdden : NO]

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

Then write all the methods required for table:

three methods are must :

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

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

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

Try this....and let me know if something query occur

as you said storyboard :

First go through the iphone story board and create your view then add your button

after that

  • drag and drap your table view
  • connect your button to the table view with module -click on table and from the menu choose-->editor-->embed in --> and add Navigation controller

with this process you can easily have your GUI demo without writing the code and then you should create the TableViewController file and add your code ,

just don't forget --> when you finished the GUI part and code, you should add createViewController class to your GUI via object in custom class.

hope this helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!