1

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

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

在此先感谢!(我真的是初学者)

第一视图控制器.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@end

第一视图控制器.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

创建视图控制器.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

编辑:我用故事板

4

5 回答 5

2

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

在 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;
}
于 2012-04-24T11:42:01.513 回答
1

第一视图控制器.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
于 2012-04-24T13:01:54.373 回答
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

于 2012-04-24T12:03:46.337 回答
0

正如你所说的故事板:

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

在那之后

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

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

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

希望这可以帮助!

于 2012-04-24T23:22:17.933 回答
0

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

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

如果你不能得到塔问我....我很乐意帮助你

于 2012-04-24T11:12:56.283 回答