iPhone:在模态视图选项卡栏中创建滚动图像/文本视图

发布于 2024-11-01 01:33:23 字数 5320 浏览 1 评论 0原文

我刚刚开始使用 Objective-C,正在寻找一种方法来创建一个 iPhone 应用程序,该应用程序将通过包含大约 8 或 9 个按钮的主菜单启动。

按下按钮时,它将链接到滚动文本视图(包含好几段文本),并在屏幕顶​​部附带图像,该图像将充当“后退”按钮以返回主菜单。我还希望它能够作为模态视图进行动画/行为(新视图从屏幕底部向上滚动,并在关闭/按下后退按钮时向下滚动)。

我已经让选项卡视图在模态视图中工作(通过按主菜单中的按钮调出),并且已经弄清楚如何为每个选项卡分配自定义图标。我还为每个选项卡添加了自定义背景。尽管我仍然无法向每个选项卡添加滚动视图,但我可以在其中以编程方式插入图片和文本。我真的很感激你能给我的任何帮助。

非常感谢!

.h.m

    #import <UIKit/UIKit.h>

@interface MasseurViewController : UIViewController {
    UITabBarController *tbc;
}

-(IBAction)showHeadTabBar;
-(void)dismissTabBar;

@property (nonatomic, retain) UITabBarController *tbc;

@end

#import "MasseurViewController.h"

@implementation MasseurViewController

@synthesize tbc;

//---Implement all the possible Tab Bar views


-(IBAction)showHeadTabBar{

    //---Define Back button

    UIImage *backImage = [UIImage imageNamed:@"headBackButton.png"];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0.0f, 0.0f, backImage.size.width, backImage.size.height);
    [button setImage:backImage forState:UIControlStateNormal];
    [button addTarget:self action:@selector(dismissTabBar) forControlEvents:UIControlEventTouchUpInside];

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(0.0f, 0.0f, backImage.size.width, backImage.size.height);
    [button2 setImage:backImage forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(dismissTabBar) forControlEvents:UIControlEventTouchUpInside];

    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    button3.frame = CGRectMake(0.0f, 0.0f, backImage.size.width, backImage.size.height);
    [button3 setImage:backImage forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(dismissTabBar) forControlEvents:UIControlEventTouchUpInside];

    //------------************TABS**************-------------------
    //-------------------------------------------------------------

    //---------------Declare View Controllers-----------
    //---------------------------TAB 1
    UIViewController *blueController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    blueController.title = @"Blue";
    blueController.tabBarItem.image = [UIImage imageNamed:@"tabBar1.png"];
    //---Add tabs & Tab names
    [blueController.view addSubview:button];
    blueController.view.backgroundColor = [UIColor whiteColor];
    blueController.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"mainMenuBKG.png"]];   


    //----TAB 2
    UIViewController *redController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    redController.view.backgroundColor = [UIColor grayColor];
    redController.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"shouldersImage1.png"]];    



    //----TAB 3
    UIViewController *greenController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    greenController.view.backgroundColor = [UIColor greenColor];
    //--------------------------------------------------


    //---Instantiate tab bars

    tbc = [[UITabBarController alloc] initWithNibName:nil bundle:nil];

    //---Create array of tabs
    tbc.viewControllers = [NSArray arrayWithObjects:blueController, redController, greenController, nil];

    //---Select starting tab
    tbc.selectedViewController = blueController;

    //--------------------------------------------------


    //---Add tabs & Tab names

    [redController.view addSubview:button2];
    redController.title = @"Red";

    [greenController.view addSubview:button3];
    greenController.title = @"Green";


    //---Release Tab views
    [blueController release];
    [redController release];
    [greenController release];
    [self presentModalViewController:tbc animated:YES];


}

//---Code to dismiss Tab Bars
- (void)dismissTabBar {
    [self dismissModalViewControllerAnimated:YES];
    [tbc release];
}


/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

I've just begun Objective-C and am looking to find a way to create an iPhone app that will launch with a Main Menu consisting of maybe around 8 or 9 buttons.

When the buttons are pressed it will link to a Scrolling Text view (containing a good few paragraphs of text) with an accompanying image at the top of the screen, which will act as a "Back" button to return to the Main Menu. I was also looking it to animate/behave as a modalview (where the new view scrolls up from the bottom of the screen, and scrolls back down when dismissed/Back-button pressed).

I have got the Tab Views working within a modal view (which is brought up from pressing a button in the Main Menu) and have worked out how to assign a custom icon to each tab. I've also added a custom background for each Tab. Though I'm still having trouble adding scrolling views to each Tab, where I can insert pictures and text programmatically. I would really appreciate any help you could give me with this..

Thanks very much!!

.h

    #import <UIKit/UIKit.h>

@interface MasseurViewController : UIViewController {
    UITabBarController *tbc;
}

-(IBAction)showHeadTabBar;
-(void)dismissTabBar;

@property (nonatomic, retain) UITabBarController *tbc;

@end

.m

#import "MasseurViewController.h"

@implementation MasseurViewController

@synthesize tbc;

//---Implement all the possible Tab Bar views


-(IBAction)showHeadTabBar{

    //---Define Back button

    UIImage *backImage = [UIImage imageNamed:@"headBackButton.png"];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0.0f, 0.0f, backImage.size.width, backImage.size.height);
    [button setImage:backImage forState:UIControlStateNormal];
    [button addTarget:self action:@selector(dismissTabBar) forControlEvents:UIControlEventTouchUpInside];

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(0.0f, 0.0f, backImage.size.width, backImage.size.height);
    [button2 setImage:backImage forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(dismissTabBar) forControlEvents:UIControlEventTouchUpInside];

    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    button3.frame = CGRectMake(0.0f, 0.0f, backImage.size.width, backImage.size.height);
    [button3 setImage:backImage forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(dismissTabBar) forControlEvents:UIControlEventTouchUpInside];

    //------------************TABS**************-------------------
    //-------------------------------------------------------------

    //---------------Declare View Controllers-----------
    //---------------------------TAB 1
    UIViewController *blueController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    blueController.title = @"Blue";
    blueController.tabBarItem.image = [UIImage imageNamed:@"tabBar1.png"];
    //---Add tabs & Tab names
    [blueController.view addSubview:button];
    blueController.view.backgroundColor = [UIColor whiteColor];
    blueController.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"mainMenuBKG.png"]];   


    //----TAB 2
    UIViewController *redController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    redController.view.backgroundColor = [UIColor grayColor];
    redController.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"shouldersImage1.png"]];    



    //----TAB 3
    UIViewController *greenController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    greenController.view.backgroundColor = [UIColor greenColor];
    //--------------------------------------------------


    //---Instantiate tab bars

    tbc = [[UITabBarController alloc] initWithNibName:nil bundle:nil];

    //---Create array of tabs
    tbc.viewControllers = [NSArray arrayWithObjects:blueController, redController, greenController, nil];

    //---Select starting tab
    tbc.selectedViewController = blueController;

    //--------------------------------------------------


    //---Add tabs & Tab names

    [redController.view addSubview:button2];
    redController.title = @"Red";

    [greenController.view addSubview:button3];
    greenController.title = @"Green";


    //---Release Tab views
    [blueController release];
    [redController release];
    [greenController release];
    [self presentModalViewController:tbc animated:YES];


}

//---Code to dismiss Tab Bars
- (void)dismissTabBar {
    [self dismissModalViewControllerAnimated:YES];
    [tbc release];
}


/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧故 2024-11-08 01:33:23

你的问题太笼统,无法给出一个好的答案,但我会尽力为你指出正确的方向。

您不必为每个按钮创建单独的 .h/.m/.xib - 您可能希望包含所有按钮的主菜单页面由单个视图控制器类(.h、.m)控制,并且它将在单个 .xib 文件中创建所有按钮可能会很方便。

您为每个按钮的操作创建的视图取决于您希望它执行的操作。例如,如果其中 3 个仅显示图像,您可以重复使用名为 displayImageView 的类。

我建议您让您的应用程序使用带有单个按钮和操作的单个主视图,然后弄清楚如何将其扩展到 8 或 9 个操作。

最后,我认为你有很多阅读要做。 此处 是一个很好的起点。祝你好运。

Your question is too general to illicit a good answer, but I'll try to point you in the right direction.

You do not have to create a separate .h/.m/.xib for each button - you probably want that main menu page with all the buttons to be controlled by a single view controller class (.h, .m) and it would probably be convenient to create all the buttons in a single .xib file.

The views you create for each button's action depends on what you want it to do. If 3 of them just show an image, you could re-use a class called displayImageView, for example.

I suggest you get your app working with a single main view with a single button and action, and then figure out how to expand it to 8 or 9 actions.

And finally, I think you have a lot of reading to do. Here is a good place to start. Good luck.

白云不回头 2024-11-08 01:33:23

在我看来,您所描述的内容非常适合使用导航控制器。
您的“主”视图应该是 UITableView,而不是 8 个或 9 个按钮。然后,每次用户在此表中选择一行时,您都会推送带有正确文本的详细信息视图。这意味着您不需要图像来充当“后退”按钮,因为导航控制器将自动为您提供该图像(这是左上角的箭头形后退按钮)。

尝试阅读有关 iPhone 基本应用程序设计模式的更多信息 (这是一个好的开始),如果您想要一些示例代码,请尝试 CoreDataBooks 或类似的东西。

祝你好运!

What you describe seems to me a perfect fit for using a navigation controller.
Your "main" view should be a UITableView, and not 8 or 9 buttons. Then, every time that the user selects a row in this table, you push a detail view with the proper text. That means that you don't need an image to act as a "back" button, as this will be provided for you automatically by the navigation controller (it's this arrow-shaped back button on the top left corner).

Try to read more about basic application design patterns for the iPhone (this is a good start) and if you want some sample code, try CoreDataBooks or something similar.

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文