1

I have a button in my view controller and I want to connect my button to table view controller (It means that when I click on button it loads and brings my table)

but I don't know how should I do that(I can connect my button to another view controller but not to the table ), here is my code

Thanks in advance!(I'm really beginner)

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 //my table

 #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

Edit: I used story board

Jacob
  • 77,566
  • 24
  • 149
  • 228

5 Answers5

2

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;
}
Prasad G
  • 6,702
  • 7
  • 42
  • 65
  • Thanks, but,I have this error: type name required specifier or qualifier xcode is there and id()-->somthing missing for this one -(IBOutlet)clickButton? –  Apr 24 '12 at 12:17
  • 1
    Oh! Sorry. Sharjeel edited my code and check it. If you have any error then write -(IBAction)clickButton:(id)sender instead of it. – Prasad G Apr 24 '12 at 12:26
  • but I have still an error in this line -(IBAction)clickButton(id)sender;{ and my error is : Expected ";" after method prototype –  Apr 24 '12 at 12:34
  • 1
    -(IBAction)clickButton:(id)sender ---- In between clickButton and (id) one semicolon(:) is there. Please put it. – Prasad G Apr 24 '12 at 12:47
  • still error here : Expected expression error in FirstViewController.m class for this line -(IBAction)click { – just one comment i using storyboard, should I connect my button to navigation controller by (push,model or ....) or should I connect button to the tableview controller ? –  Apr 24 '12 at 15:02
  • I have edited my code and fallow those steps. If you have any doubts you can ask me. – Prasad G Apr 25 '12 at 04:30
1

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
k.shree
  • 119
  • 1
  • 2
  • 12
  • Give outlets....MaintableView should be connected to tableview....Connect datasourse and delegate methods...click () will be connected to button.....Hope you will get your solution..... – k.shree Apr 24 '12 at 13:03
  • in .h file after declaring method : -(IBAction)click; Here ; is must...May be you are missing semicolon... – k.shree Apr 24 '12 at 13:08
  • now I have Expected expression error in FirstViewController.m class for this line -(IBAction)click { –  Apr 24 '12 at 13:23
  • still error here : Expected expression error in FirstViewController.m class for this line -(IBAction)click { – just one comment i using storyboard, should I connect my button to navigation controller by (push,model or ....) or should I connect button to the tableview controller ? –  Apr 24 '12 at 15:01
0

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

k.shree
  • 119
  • 1
  • 2
  • 12
  • thank you so much, but where should I put my code in UITableView , I beginner and I don't know where should I put it. –  Apr 24 '12 at 11:31
  • @justin: AS u have said that "I can connect my button to another view controller but not to the table" ;then put some code of that action method that gets executed upon button is clicked. In that method u can write line of code to pushViewController of UITableView. If u want that upon click button it should load and brings ur table; then ur table view code is not of matter her but code here u are performing action upon button click is important. Edit ur answer accordingly – hp iOS Coder Apr 24 '12 at 11:41
  • @justin: "Prasad G"s answer can help u to get solution. – hp iOS Coder Apr 24 '12 at 11:47
  • @hpiOSCoder but it doesn't work yet I have this error now : an error in this line -(IBAction)clickButton(id)sender;{ and my error is : Expected ";" after method prototype –  Apr 24 '12 at 12:35
  • @justin (IBAction)clickButton:(id)sender; write this – hp iOS Coder Apr 24 '12 at 12:50
  • @hpiOSCoder I wrote : -(IBAction)clickButton :(id)sender{ if (!createViewController) and an error is: Expected ";" after method prototype –  Apr 24 '12 at 13:01
  • @justin It says that ur missing ';' somewhere. Check where r u missing it. That line must be showing u in red or yellow color. R u declaring this method .h file, if yes then check if u have ended that line with ';'. – hp iOS Coder Apr 24 '12 at 13:05
  • @hpiOSCoder still error here : Expected expression error in FirstViewController.m class for this line -(IBAction)click { – just one comment i using storyboard, should I connect my button to navigation controller by (push,model or ....) or should I connect button to the tableview controller ? –  Apr 24 '12 at 15:02
  • @justin: Oops.. If you are using storyboard then u must read this doc https://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007457 in this doc just search for the keyword 'seagues'. It will clear ur concepts & give answer as well – hp iOS Coder Apr 24 '12 at 15:21
0

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

k.shree
  • 119
  • 1
  • 2
  • 12
  • No need to create two views....If it necessary to create two views than on button click,Move to the second view via NSDefaultUser method. – k.shree Apr 24 '12 at 12:06
  • 1
    thanks but if you check my code I already write these 3 methods but I don't know why it should be sethidden or not and also would you please change my code and write what is exactly you mean? –  Apr 24 '12 at 12:29
  • I have set it setHidden cause you want to show tableView on button_click....Got it or not? – k.shree Apr 24 '12 at 13:10
0

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!

Elnaz
  • 1,095
  • 1
  • 12
  • 30