Button switching in the navigation bar of IOS getting started development (3)

Source: Internet
Author: User

IOS entry development-navigation bar button switch

If the original Yusong Momo article is reprinted, please note: Reprinted to my independent domain name blog Yusong Momo program Research Institute, Original address: http://www.xuanyusong.com/archives/590

, Add multiple horizontal buttons in the navigation bar. You can switch back and forth to the central view of the screen by clicking the button on the Table Page.

The previous article introduced the use of pure code to add an image view. However, some friends recently asked me how to use IB to add an image view, in this article, I will introduce how to add using IB, which is actually very simple.


First drag the image resource file 0.jpg into the project, open the. XIB file corresponding to the view, as shown in, drag the imageview control into the view

Image: select the resource file displayed in the view.

Tag: indicates the index of the resource file. You can obtain the imageview object based on the index.

For more information about the creation method of the navigation bar, see the previous chapter. Next, add segmented and horizontal buttons in the navigation bar.



-(Void) viewdidload {[Super viewdidload]; // create a navigation bar uinavigationbar * navigationbar = [[uinavigationbar alloc] initwithframe: cgrectmake (0, 0,320, 44)]; // create a navigation bar set uinavigationitem * navigationitem = [[uinavigationitem alloc] initwithtitle: Nil]; // Add a horizontal button list to the navigation bar set nsarray * buttons = [nsarray arraywithobjects: @ "Yu Song Momo", @ "RuO wa", @ "Xiao cute", @ "", nil]; uisegmentedcontrol * segmentedcontrol = [[uisegmentedcontrol alloc] initwithitems: Buttons]; // set the horizontal button style segmentedcontrol. segmentedcontrolstyle = uisegmentedcontrolstylebar; // Add a button to respond to the event [segmentedcontrol addtarget: Self action: @ selector (buttonaction :) forcontrolevents: uicontroleventvaluechanged]; // Add the navigation bar set to the navigation bar, set the animation to close [navigationbar pushnavigationitem: navigationitem animated: No]; // Add the horizontal list to the navigation bar navigationitem. titleview = segmentedcontrol; // Add the navigation bar to the view [self. view addsubview: navigationbar]; // release the object [navigationitem release]; [segmentedcontrol release];}



After you click the button in the navigation bar, the buttonaction method will be executed. In this method, I will mainly talk about [self. View viewwithtag: 10]. This is to find the view object with id 10 Based on the index, which corresponds to the tag described above one by one. This is a bit like android development.


-(Void) buttonaction: (uisegmentedcontrol *) sender {// get the button and click index nsinteger Index = sender. selectedsegmentindex; // calculate the image name nsstring * Show = [nsstring stringwithformat: @ "% d % @", index ,@". jpg "]; // obtain the view object uiimageview * imageview = (uiimageview *) [self. view viewwithtag: 10]; // draw a new image [imageview setimage: [uiimage imagenamed: Show];}

Switch and click the button to see the effect. The view has changed ~~~

In the end, if you still think that I am not writing enough details, it doesn't matter if I post the source code. You are welcome to discuss and study Yu Song Momo, hoping to make progress together with everyone.

: Http://www.xuanyusong.com/archives/590

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.