在xcode中将按钮连接到TableViewController [英] connect button to TableViewController in xcode

查看:76
本文介绍了在xcode中将按钮连接到TableViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

FirstViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@end

FirstViewController.m

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//我的表

CreateViewController.h //my table

 #import <UIKit/UIKit.h>

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

 }

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

CreateViewController.m

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

我使用故事板

推荐答案

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

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

在FirstViewController.h中,

In FirstViewController.h,

#import "CreateViewController.h"

@interface FirstViewController : UIViewController{

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

在FirstViewController.m中,您只需添加以下方法

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

并在AppDelegate.h中,

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

在AppDelegate.m中,

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

这篇关于在xcode中将按钮连接到TableViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆