How to press button in View #1 to change the Label in View #2 (Xamarin iOS)

How to press button in View #1 to change the Label in View #2 (Xamarin iOS)



I when I press a button in view #1 (for example "PurpleFruitBtn") and I want to change the text in another view down the line (after a few other screens of other things) to display "Selection: Grape."



Have tried a few things, but they don't work.



How would more experienced people that know what they are doing do it?



Thanks,
A humbled kid who knows more and more that he doesn't know much.




3 Answers
3



You can define a string variable in View#2, and then put it in View#1 when you're switching to View#2,like this
enter image description here



When the page switch is complete, the label on the second page takes the variable of PhoneNumbers and displays it.



You can define a common class for transferring data between views:


using System;
namespace XamarinIosDataPass
public class CommonClass
public static string value;




Then when in view1, you update data when button is clicked:


partial void BtnSend_TouchUpInside(UIButton sender)
CommonClass.value = txtInput.Text;



And update the lable in View2 based on value of the Common class:


public override void ViewDidLoad()
base.ViewDidLoad();
lblDisplay.Text = CommonClass.value;



Please refer this link for more details.



Happy coding!



You can also use NSNotification to send message and pass value between two views.


NSNotification



in View #1



post the notification when you want to change the label in view#2


UIButton button = new UIButton(new CGRect(100, 300, 100, 50));
button.SetTitle("PurpleFruit",UIControlState.Normal);
button.SetTitleColor(UIColor.White, UIControlState.Normal);
button.BackgroundColor = UIColor.Blue;
button.TouchUpInside += (sender, e) =>


NSDictionary dic =NSDictionary.FromObjectAndKey(new NSString("value"),new NSString(button.TitleLabel.Text)) ;

NSNotification notification = NSNotification.FromName("ChangeValue",null,dic); //pass the string as a parameter

NSNotificationCenter.DefaultCenter.PostNotification(notification);

;



in view #2



Register the notification in the method ViewWillAppear


ViewWillAppear


public override void ViewWillAppear(bool animated)

base.ViewWillAppear(animated);

NSNotificationCenter.DefaultCenter.AddObserver(this,new Selector("ChangeValue:"),new NSString("ChangeValue") ,null);


[Export("ChangeValue:")]
public void ChangeValue(NSNotification notification)

Console.Write(notification.UserInfo.ObjectForKey(new NSString ("value")));
// now you can change the label.text



Notice :the name of notification must be same (just like ChangeValue in above code). Before posting you should register the notification firstly.Otherwise,view #2 can't receive the notification.


ChangeValue



For more details about NSNotification you can refer to here



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế