我的视图控制器中有一个按钮,我想将我的按钮连接到表格视图控制器(这意味着当我点击按钮时它会加载并带来我的表格)
但我不知道该怎么做(我可以将我的按钮连接到另一个视图控制器而不是表格),这是我的代码
提前致谢!(我真的是初学者)
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 //我的表
#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
编辑:我使用了故事板
答案 0 :(得分:2)
我认为该按钮位于FirstViewController中。如果是实现 - (IBAction)clickButton并编写代码并将其连接到Interface Builder中的底部(如果使用Interface Builder)。在FirstViewController.h中编写createViewController
对象和#import <CreateViewController.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;
}
答案 1 :(得分: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
答案 2 :(得分:0)
不要将您的UITableView代码放在viewDidLoad()方法中。
创建一个按钮并在加载时显示...按钮的操作方法显示表并加载数据...
如果你不能问我......我很乐意帮助你
答案 3 :(得分:0)
在第一个视图中,创建一个名为btn1的按钮。
将其设置在模拟器屏幕的顶部。
现在从对象库中获取tableview。把它放在按钮下.....
现在在加载时间使表格隐藏....
写[tablename sethidden:YES]
现在,按钮的动作方法,[tablename sethdden:NO]
tableData = [[NSArray alloc] initWithObjects:@“Johan”,@“Paul”,@“George”,@“Ringo”,nil];
然后编写表所需的所有方法:
必须采用三种方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
尝试这个....并告诉我是否发生了查询
答案 4 :(得分:0)
正如你所说的故事板:
首先浏览iphone故事板并创建视图,然后添加按钮
之后
使用此过程,您可以轻松地进行GUI演示而无需编写代码,然后您应该创建TableViewController文件并添加代码,
不要忘记 - &gt;完成GUI部分和代码后,应该通过自定义类中的对象将createViewController类添加到GUI中。
希望这有帮助!