Displaying input text on different views - string

Was wondering if anybody could help on a simple problem
I have a UITextView on one page, where I want to enter text and store it in a string. Then when the next page is accessed, I would like that text to be displayed on that page, either in a new UITextView or some other way.
In my PostViewController.swift file (the first page) to get the input string, I have
#IBOutlet weak var postTextView: UITextView!
I tried making a new UITextView on the other view controller to try and display the string, and had the subsequent code, but can't figure it out from here.
#IBOutlet weak var copyText: UITextView!
let text: NSString = PostViewController().postTextView.text
???copyText = text??? (not sure if this is even close to right)
Any ideas would be appreciated and apologies for the noob question

In your PostViewController, implement prepareForSegue() to assign the value of its textfield to the variable in the second controller.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showSecondVC" { // Make sure the segue has this identity in Interface Builder
let vc = segue.destinationViewController as! SecondVC // Change that to the actual class name
vc.textFromVC1 = postTextView.text
}
}
In your second ViewController:
class SecondviewController: UIViewController {
var textFromVC1: String!
#IBOutlet weak var myTextField: UITextField!
override func viewDidLoad() {
myTextField.text = textFromVC1
}
}

Related

NSManagedObject inserted in context, but not shown in tableview

Using Core Data in my App, I have a strange behaviour. I can add and remove objects using FirstResponder, and the objects are shown immediately in my tableView.
But if I want to add Objects programmatically , objects are only registered and not saved - nor shown in the tableView.
What I did :
Creating the PersistentContainer
class ViewController: NSViewController {
#IBOutlet var arrayCrtl: NSArrayController!
var container: NSPersistentContainer!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
container = NSPersistentContainer(name:"Document")
container.loadPersistentStores(completionHandler: { (storeDescription, error ) in
if let error = error { print ("\(error)") }
})
}
Then, programmatically adding a ManagedObject
let context = container.viewContext
let describer = NSEntityDescription.entity(forEntityName: "Event", in: context)!
let newEvent:Event = Event(entity:describer, insertInto:context)
container.viewContext.insert(newEvent)
print ( container.viewContext.registeredObjects)
Any idea what's missing ?
I simply forgot to set the ArrayController to "prepares content" in the attribute inspector:

How to send back to custom class which object of that class was clicked

I am stack at this problem can't figure out how to do it properly. [Haxe/OpenFL]
I want to make following menu. On screen for player display three images/buttons. When player clicks on one of the images/buttons a text with description appears under that button.
My is, i dont know how to send from Main (where i create this buttons and using them), send info to custom class of this images/buttons, what specific button/image was pressed.
Here is example code, first from custom class of the images/buttons:
class CusstomButtons extends Sprite {
var buttonImagePath:String;
var _buttonImagePath:String;
var buttonName:String;
var _buttonName:String;
var button1Btmp:Bitmap = new Bitmap ();
var button1Sprt:Sprite = new Sprite();
var button2Btmp:Bitmap = new Bitmap ();
var button2Sprt:Sprite = new Sprite();
var buttonText1:TextField = new TextField ();
var buttonText2:TextField = new TextField ();
public function new(buttonImagePath, buttonName) {
super();
_buttonImagePath = buttonImagePath;
_buttonName = buttonName;
createButton ();
}
public function createButton () :Void {
if (_buttonName == Button1){
button1Btmp = new Bitmap (Assets.getBitmapData (_buttonImagePath));
button1Sprt.addChild(button1Btmp);
addChild(button1Sprt);
//Here goes the code for button position and tweening
}
if (_buttonName == Button2){
button2Btmp = new Bitmap (Assets.getBitmapData (_buttonImagePath));
button2Sprt.addChild(button2Btmp);
addChild(button2Sprt);
//Here goes the code for button position and tweening
}
}
public function displayButtonDescrition () :Void {
if (button1) {
buttonText1.text = "Some text for Button 1"
addChild(buttonText1);
//Here goes code for button position and etc
}
if (button2) {
buttonText2.text = "Some text for Button 2"
addChild(buttonText2);
//Here goes code for button position and etc
}
}
}
And here is code from main:
class Main extends Sprite {
public var button1Menu:CusstomButtons;
public var button2Menu:CusstomButtons;
public function new () {
super ();
button1Menu = new CusstomButtons ("path/button1", "button1");
button1Menu = new CusstomButtons ("path/button1", "button2");
}
public function createButtonMenu ():Void {
button1Menu.createButton();
addChild(button1Menu);
button2Menu.createButton();
addChild(button2Menu);
button1Menu.addEventListener(MouseEvent.CLICK, onClick);
button2Menu.addEventListener(MouseEvent.CLICK, onClick);
}
public function onClick (event:MouseEvent):Void {
if (event.currentTarget == button1Menu) {
button1Menu.displayButtonDescrition();
}
if (event.currentTarget == button2Menu) {
button2Menu.displayButtonDescrition();
}
}
}
The main question is how to use one function to display different description texts.
You could make a static field in the Button class to hold all created instances of it.
static var instances = new Array();
Then in the Button constructor store the instance that is currently being created
instances.push(this);
Finally, from the main class call a static method in the button class, passing the clicked instance:
public static function setClickedInstance(instance:Button):Void{
//manipulation code goes here
}
And in the main class call the method when necessary:
Button.setClickedInstance();
Sorry if above doesn't compile as I couldn't test it right now. If not, it probably just needs a bit of tweaking.
An alternative would be to add a mouse listener in the Button class itself, but then you wouldn't have control in the main class when to "react" on clicks and when not.

Simple Swift WebView not working (Xcode 6 Beta 3)

I was writing a simple WebView in Swift, but everytime I try to launch it in the iOS Simulator I get these errors. What is going wrong?
import UIKit
class ViewController: UIViewController {
#IBOutlet var webview: UIWebView
var urlpath = "http://www.google.de"
func loadAddressURL(){
let requesturl = NSURL(string: urlpath)
let request = NSURLRequest(URL: requesturl)
webview.loadRequest(request)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
loadAddressURL()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Error:
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
self uiwebview.ViewController 0x7987fc70 0x7987fc70
request NSURLRequest * 0x78ebfc40 0x78ebfc40
requesturl NSURL * "" 0x78ec0040
You simply haven't connected your UIWebView to the webview class property
Open the assistant editor, show your xib or storyboard at left, your view controller source file at right, click on the circle at the left of the webview property and drag into the UIWebView control. Once the connection is established, run the app and it should work now
I guess you get a white page because you test on the simulator. If you test on a real device you should be fine.
You need to put "!" after 'UIWebView' and unwrap "requesturl" to get your String value otherwise it's been an optional and you get error.
import UIKit
class WebViewController: UIViewController {
#IBOutlet var webview: UIWebView!
var urlpath: String = "http://www.google.de"
func loadAddressURL(){
let requesturl = NSURL(string: urlpath)
let request = NSURLRequest(URL: requesturl!)
webview.loadRequest(request)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
loadAddressURL()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Monotouch.Dialog RootElement that opens a UIViewController and Passing in Data

Is there a proper way to use Monotouch.Dialog (iOs) and call a UIViewController when a user clicks on a RootElement? I'm building a page of data based on an array and when clicked I'd like to open this custom view and pass in the array element.. Something like this (doesn't work). Any help is appreciated.
RootElement CreateMenuCategory(JToken menucat) {
RootElement MenuCategory = new RootElement(menucat["menucategoryname"].Value<String>());
RootElement root_element;
Section section = new Section();
foreach(JToken menuitem in menucat["menuitems"]) {
root_element = new RootElement(menuitem["menuitemname"].Value<String>(), (RootElement e) => {
return _menuitemView.LoadMenuItem(menuitem); // menuitem on view is always the same
});
section.Add (root_element);
}
MenuCategory.Add (section);
return MenuCategory;
}
That code doesn't work as the delegate always passes in the same element every time.
This is merely a side effect of how the "menuitem" variable is captured by the lambda function.
Change your foreach loop to look like this instead:
foreach (JToken iteratorMenuitem in menucat ["menuitems"]){
var menuitem = iteratorMenuitem;
//.. the rest

How can I animate a View to fly in from the bottom of the screen?

I'm trying to figure out how to have a view. Let's call it ThirdView. It should slide up from the bottom of the screen when a user clicks a particular button on SecondView.
You'll want to create the ThirdView in your SecondView and present it as a modal view, passing in the secondView in the constructor. This will be the easiest way of animating it in the way you would like.
var thirdView = new ThirdView(secondView);
this.PresentModalViewController(thirdView, true);
In your third view, you'll want to call the passed-in SecondView and call:
secondView.DismissModalViewControllerAnimated(true);
Here is a complete working example. It is a tad simpler than in chrisntr's answer...though the above example is what I used to figure everything out.
The coolest thing about this method is that for an artistic custom UI (like the one I am building for a game), there is no off-the-shelf UI elements like the TabBar, Navigation bars, etc. The most creative applications don't use standard UI stuff.
In your main.cs file, in your finishedlaunching block:
ViewController myUIV = new ViewController();
window.AddSubview(myUIV.View);
window.MakeKeyAndVisble();
And then in a new code file add this code:
using System;
using System.Drawing;
using MonoTouch.UIKit;
namespace AnimationTest
{
public class ViewController : UIViewController
{
UIButton uib = new UIButton(new RectangleF(100, 100, 40, 40));
public override void ViewDidLoad()
{
Console.WriteLine("UI1");
this.View.BackgroundColor = UIColor.Blue;
uib.BackgroundColor = UIColor.White;
uib.TouchUpInside += delegate {
Console.WriteLine("Hey!");
var vc2 = new SecondController();
PresentModalViewController(vc2, true);
};
this.View.AddSubview(uib);
base.ViewDidLoad();
}
}
public class SecondController : UIViewController
{
UIButton uib = new UIButton(new RectangleF(100, 100, 40, 40));
public override void ViewDidLoad()
{
this.View.BackgroundColor = UIColor.White;
uib.BackgroundColor = UIColor.Red;
uib.TouchUpInside += delegate {
this.DismissModalViewControllerAnimated(true);
};
this.View.AddSubview(uib);
base.ViewDidLoad();
}
}

Resources